我有以下模特管理员。我在列表视图中显示自定义字段。
class CustomerAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'email', 'state')
search_fields = ('first_name', 'last_name', 'email')
list_filter = ('state',)
def state(self, obj):
address = Address.objects.filter(owner=obj.id)
if address:
return address.state
return None
我在上面试过但是它给出了一个错误“list_filter [0]”指的是'state',它没有引用Field。“ 所以我希望按状态过滤记录。那我怎么能在django 1.5中做到这一点?
答案 0 :(得分:-1)
class CustomerAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'email', 'state')
list_filter = ('state',)
search_fields = ('first_name', 'last_name', 'email')
def state(self, obj):
address = Address.objects.filter(owner=obj.id)
if address:
return address.state
return None
如果要过滤
,则应包括list_filter