【问题标题】:get all the items that belong to yellow boxes in Django models获取 Django 模型中属于黄色框的所有项目
【发布时间】:2012-11-21 16:14:42
【问题描述】:

所以我有这个我正在尝试做的事情的简化版本,但失败了。 我有这个具有颜色字段的 Boxes 对象,并且可以通过 m2m 字段包含许多项目,所以我想要一个特定颜色框中所有项目的列表(一个查询集)。所以我为管理员制作了这个 django 过滤器。

from django.contrib.admin import SimpleListFilter
from store.models import Box

class Items(Model):
    name = CharField(max_length=200)

class Box(Model):
    items = ManyToManyField(Items)
    color_choices = (
        ('yellow','yellow'),
        ('green','green'),
    )
    box_color = CharField(max_length=200,choices=color_choices)

# So in my Items admin list i want to filter the items by color, this is my 'failed' attemp.

class Items_by_payment_system_filter(SimpleListFilter):
    title = _('Colors')

    # Parameter for the filter that will be used in the URL query.
    parameter_name = 'color'

    def lookups(self, request, model_admin):
        """
        Returns a list of tuples. The first element in each
        tuple is the coded value for the option that will
        appear in the URL query. The second element is the
        human-readable name for the option that will appear
        in the right sidebar.
        """
        return Box.color_choices
    def queryset(self, request, queryset):
        # Remember that self.value contains the current selection in the filter options displayed to the user in Admin
        if self.values():
            boxes = Box.objects.filter(items__in=queryset) # get only boxes with the current set of items
            boxes_ids = boxes.filter(box_color=self.values()).values_list('items__id',flat=True)
            return queryset.filter(id__in=boxes_ids)

我不确定出了什么问题,因为管理员向我展示了属于与我选择的颜色不同的盒子的物品。

【问题讨论】:

  • 不应该是return Box.box_color.choices吗?还是您也将颜色存储在 Box.colors 中?
  • 是的,抱歉,更新了代码,因为是的,它是错误的,它没有反映我在真实代码中的内容。
  • 那么 self.values() 返回一个值? -- 你做的是box.filter(color=self.values()),这不应该是box.filter(box_color=self.values())吗?
  • 对代码的注释:始终使您的模型名称为单数,而不是复数。所以Item 而不是Items

标签: python django filter django-queryset m2m


【解决方案1】:

这是你的问题:

  • if self.values(): 中的self.values 更改为self.value
  • boxes_ids = boxes.filter(color=self.values()) ... 中的color 更改为box_color

代码示例(在 django 1.4.2 上测试和工作)

from box.models import Items,Box
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin import SimpleListFilter

class Items_by_payment_system_filter(SimpleListFilter):
    title = _('Colors')

    # Parameter for the filter that will be used in the URL query.
    parameter_name = 'color'

    def lookups(self, request, model_admin):
        """
        Returns a list of tuples. The first element in each
        tuple is the coded value for the option that will
        appear in the URL query. The second element is the
        human-readable name for the option that will appear
        in the right sidebar.
        """
        return Box.color_choices
    def queryset(self, request, queryset):
        # Remember that self.value contains the current selection in the filter options displayed to the user in Admin

        if self.value():
            boxes = Box.objects.filter(items__in=queryset) # get only boxes with the current set of items
            boxes_ids = boxes.filter(box_color=self.value()).values_list('items__id',flat=True)
            return queryset.filter(id__in=boxes_ids)


class ItemsAdmin(admin.ModelAdmin):
    fields = ['name']
    list_filter = (Items_by_payment_system_filter,)

admin.site.register(Items,ItemsAdmin)
admin.site.register(Box)

【讨论】:

  • 是的,我得到了这些错误,它们在翻译错误中丢失了。我还在调查这个问题。但不是导致错误结果的原因。
猜你喜欢
  • 2013-06-08
  • 2020-12-18
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
  • 2014-05-13
相关资源
最近更新 更多