Printer
Printer

Reputation: 465

Django admin list_filter custom field error

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.

Upvotes: 6

Views: 3371

Answers (2)

Muneer Khan
Muneer Khan

Reputation: 164

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

Upvotes: 4

bdoubleu
bdoubleu

Reputation: 6127

You have to specify which model department is in.

list_filter = ('employee__department',)

Upvotes: 2

Related Questions