获取属于Django模型中的黄色框的所有项目
问题描述:
因此,我有这个简化版本的我正试图做和失败。 我有这个Boxes对象有一个颜色字段,并且可以有一个m2m字段的许多项目,所以我想要一个列表(一个查询集)的所有在特定颜色的框中的项目。所以我为管理员制作了这个django过滤器。获取属于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)
我不知道什么是错的,因为管理是显示我属于同一个比我选择其他不同颜色的盒子项目。
答
这是你的问题:
- 变化
self.values
到self.value
在if self.values():
- 变化
color
到box_color
在boxes_ids = boxes.filter(color=self.values()) ...
代码示例(测试和工作在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)
+0
是的,我得到了这些错误,他们在翻译错误中丢失了。我仍在调查这个问题。但不是错误结果的原因。 –
不应该是'返回Box.box_color.choices'?或者你是否也在Box.colors中存储了颜色? – SiddharthaRT
对不起,更新了代码,因为是的,它是错误的,它并没有反映我在我的真实代码中有什么。 –
So self.values()返回一个值? - 你做boxes.filter(color = self.values()),不应该是boxes.filter(box_color = self.values())? – Teisman