Creating Custom Filters in Django Admin

Django
2017-02-26 19:25 (7 years ago) ytyng

Using SimpleListFilter for Easy Implementation

from django.contrib import admin
class TicketStatusListFilter(admin.SimpleListFilter):
title = 'Status'
parameter_name = 'status_set'

def lookups(self, request, model_admin):
return (
('incomplete', 'Incomplete'),
('completed', '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,)

Current rating: 2.3

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011