我希望django仅在过滤项目时显示过滤器中的相关字段。
例如,如果我选择品牌“XYZ”,它只会在过滤器中显示包含“XYZ”品牌的选项。
我的代码来自这里的另一个问题 - 但它不起作用。我一直收到“NotRelationField”错误(http://dpaste.com/23Y8ZE3)。
Admin.py ---
from django.contrib.admin.filters import RelatedOnlyFieldListFilter
# Some SimpleListFilter filters
class ProductAdmin(admin.ModelAdmin):
actions = ['tag_Active_Wear', 'tag_Trending',]
list_filter = (
('brand', RelatedOnlyFieldListFilter),
)
admin_order_field = ('price',)
# Some querysets for the actions
admin.site.register(Product, ProductAdmin)
Models.py ----
class Product(models.Model):
name = models.CharField ("Name", max_length=400)
store = models.ForeignKey(Store)
brand = models.CharField("Brand", max_length=200, blank=True)
category = models.ManyToManyField(Category, blank=True)
def __unicode__(self):
return self.name
答案 0 :(得分:2)
应该是store
而不是brand
,因为brand
不是RelatedField
list_filter = (
('store', RelatedOnlyFieldListFilter),
)