Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin list_filter custom field error

Tags:

django

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:

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

Refrence to how it looks: https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#extending-user

With this addition:

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

List display seems work fine.

like image 737
Printer Avatar asked Jun 20 '18 17:06

Printer


2 Answers

User model has relationship with Employee Model so you can access it From user Table with employee field. employee is now the field of user.

list_filter = ('employee__department',)

employee is the Model. and department is the field of Employee. hopes it should work for you My one is working below is the screen shot the that.

this is my model Screenshot  check it out.

this is admin screenshot and filters

and this is the final resutls

like image 102
Muneer Khan Avatar answered Nov 10 '22 20:11

Muneer Khan


You have to specify which model department is in.

list_filter = ('employee__department',)
like image 2
bdoubleu Avatar answered Nov 10 '22 20:11

bdoubleu