Django管理员list_filter自定义字段错误 [英] Django admin list_filter custom field error

查看:107
本文介绍了Django管理员list_filter自定义字段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过其他选项(例如部门)扩展了用户。但是,当我尝试在管理面板上为derpartment添加过滤器时。它将引发以下错误:

I extended the user with extra option, such as department. But when i try adding filter for the derpartment at the admin panel. It throws this error:


错误:
:(admin.E116) list_filter [0]的值引用

ERRORS: : (admin.E116) The value of 'list_filter[0]' refers to 'department', which does not refer to a Field.

请参考其外观: https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#extending -用户

添加以下内容:

class UserAdmin(BaseUserAdmin):
inlines = (EmployeeInline, )
list_display = ('username', 'email', 'first_name', 'last_name', 'get_department')
list_filter = ('department',)

def get_department(self, instance):
    return instance.employee.department

列表显示似乎正常。

推荐答案

您必须指定哪种型号部门在。

You have to specify which model department is in.

list_filter = ('employee__department',)

这篇关于Django管理员list_filter自定义字段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆