参见英文答案 >
Default filter in Django admin13个
有没有办法在管理员中设置过滤器的默认值
现场?
有没有办法在管理员中设置过滤器的默认值
现场?
Class Account(models.model): isClosed = models.BooleanField(blank=True) name = models.CharField(core=True,max_length=255,unique=True)
在admin.py中:
class Admin(admin.ModelAdmin): list_filter = ['isClosed']
在管理页面上,我想要’isClosed’字段的默认值
是’不’,不是’全’.
谢谢.
解决方法
您需要创建一个自定义列表过滤器,继承自django.contrib.admin.filters.SimpleListFilter并默认过滤未关闭的帐户.这些方面的东西应该有效:
from datetime import date from django.utils.translation import ugettext_lazy as _ from django.contrib.admin import SimpleListFilter class IsClosedFilter(SimpleListFilter): title = _('Closed') parameter_name = 'closed' def lookups(self,request,model_admin): return ( (None,_('No')),('yes',_('Yes')),('all',_('All')),) def choices(self,cl): for lookup,title in self.lookup_choices: yield { 'selected': self.value() == lookup,'query_string': cl.get_query_string({ self.parameter_name: lookup,},[]),'display': title,} def queryset(self,queryset): if self.value() == 'closed': return queryset.filter(isClosed=True) elif self.value() is None: return queryset.filter(isClosed=False) class Admin(admin.ModelAdmin): list_filter = [isClosedFilter]