SimpleListFilter を使って簡単に書ける
from django.contrib import admin
class TicketStatusListFilter(admin.SimpleListFilter):
title = 'ステータス'
parameter_name = 'status_set'
def lookups(self, request, model_admin):
return (
('incomplete', '完了以外'),
('completed', '完了'),
)
def queryset(self, request, queryset):
if self.value() == 'incomplete':
return queryset.exclude(status='completed')
if self.value() == 'completed':
return queryset.filter(status='completed')
class TicketAdmin(admin.ModelAdmin):
list_filter = (TicketStatusListFilter,)
コメント