I'm trying to filter by the custom method i made for language
column. In admin.py
i have the following;
from django.contrib import admin
from .models import *
class MovieAdminModel(admin.ModelAdmin):
search_fields= ('title_en')
list_display = ('get_language',)
list_filter = ('get_language')
def get_language(self, obj):
if obj.language==1:
return 'Persian With English Subtitle'
else:
return 'Persian'
get_language.admin_order_field = "language"
get_language.short_description = "language"
In list_filter
i use get_language
and this error is returned The value of 'list_filter[0]' refers to 'get_language', which does not refer to a Field. If i use language
in list_filter
, the filters shows yes
or no
. Filter should show either Persian With English Subtitle
or Persian
. How do i solve?
CodePudding user response:
You need to create a custom admin filter for this task, something like this:
from django.contrib.admin import SimpleListFilter
class LanguageFilter(SimpleListFilter):
title = 'Language'
parameter_name = 'language'
def lookups(self, request, model_admin):
return [
('Persian With English Subtitle', 'Persian With English Subtitle'),
('Persian', 'Persian')
]
def queryset(self, request, queryset):
value = self.value()
if value == 'Persian With English Subtitle':
return queryset.filter(language=1)
return queryset.exclude(language=1)
class MovieAdminModel(admin.ModelAdmin):
list_filter = (
LanguageFilter,
)
...