本文整理汇总了Python中django.contrib.admin.SimpleListFilter方法的典型用法代码示例。如果您正苦于以下问题:Python admin.SimpleListFilter方法的具体用法?Python admin.SimpleListFilter怎么用?Python admin.SimpleListFilter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.admin
的用法示例。
在下文中一共展示了admin.SimpleListFilter方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: formfield_for_foreignkey
# 需要导入模块: from django.contrib import admin [as 别名]
# 或者: from django.contrib.admin import SimpleListFilter [as 别名]
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if request.user.is_superuser:
return super().formfield_for_foreignkey(db_field, request, **kwargs)
if db_field.name == "user":
kwargs["queryset"] = User.objects.filter(username=request.user.username)
return super().formfield_for_foreignkey(db_field, request, **kwargs)
# class DurationFilter(admin.SimpleListFilter):
# title='Duration'
# parameter_name='calculated_duration'
#
# def lookups(self, request, queryset):
# return(
# ('1','More than 3 hours'),
# ('2','Less than 3 hours'),
# )
#
# def queryset(self, request, queryset):
# value = self.value()
# if value == '1':
# return queryset.filter(xduration__gt=datetime.timedelta(hours=3))
# elif value == '2':
# return queryset.exclude(xduration__gt=datetime.timedelta(hours=3))
# return queryset
示例2: test_not_filter_again_again
# 需要导入模块: from django.contrib import admin [as 别名]
# 或者: from django.contrib.admin import SimpleListFilter [as 别名]
def test_not_filter_again_again(self):
class AwesomeFilter(SimpleListFilter):
def get_title(self):
return 'awesomeness'
def get_choices(self, request):
return (('bit', 'A bit awesome'), ('very', 'Very awesome'))
def get_queryset(self, cl, qs):
return qs
class TestModelAdmin(ModelAdmin):
list_filter = (('is_active', AwesomeFilter),)
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'list_filter[0][1]' must inherit from 'FieldListFilter'.",
'admin.E115'
)
示例3: test_valid_case
# 需要导入模块: from django.contrib import admin [as 别名]
# 或者: from django.contrib.admin import SimpleListFilter [as 别名]
def test_valid_case(self):
class AwesomeFilter(SimpleListFilter):
def get_title(self):
return 'awesomeness'
def get_choices(self, request):
return (('bit', 'A bit awesome'), ('very', 'Very awesome'))
def get_queryset(self, cl, qs):
return qs
class TestModelAdmin(ModelAdmin):
list_filter = ('is_active', AwesomeFilter, ('is_active', BooleanFieldListFilter), 'no')
self.assertIsValid(TestModelAdmin, ValidationTestModel)
示例4: dashboard_link
# 需要导入模块: from django.contrib import admin [as 别名]
# 或者: from django.contrib.admin import SimpleListFilter [as 别名]
def dashboard_link(self, obj):
return format_html('<a href="{}">view in site</a>', obj.dashboard_url())
# See Django documentation for SimpleListFilter:
# https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
示例5: countFilter
# 需要导入模块: from django.contrib import admin [as 别名]
# 或者: from django.contrib.admin import SimpleListFilter [as 别名]
def countFilter(field_name):
class CountFilter(SimpleListFilter):
title = "%s Count" % field_name.title()
parameter_name = "%s_count" % field_name
def lookups(self, request, model_admin):
return (
("0", "0"),
("1", "1"),
("2", "2 - 9"),
("10", "10 - 99"),
("100", "100+"),
(">0", "> 0"),
)
def queryset(self, request, queryset):
if self.value() == "0":
return queryset.annotate(num_events=Count(field_name)).filter(
num_events=0
)
if self.value() == ">0":
return queryset.annotate(num_events=Count(field_name)).filter(
num_events__gt=0
)
if self.value() == "1":
return queryset.annotate(num_events=Count(field_name)).filter(
num_events=1
)
if self.value() == "2":
return queryset.annotate(num_events=Count(field_name)).filter(
num_events__gte=2, num_events__lte=9
)
if self.value() == "10":
return queryset.annotate(num_events=Count(field_name)).filter(
num_events__gte=10, num_events__lte=99
)
if self.value() == "100":
return queryset.annotate(num_events=Count(field_name)).filter(
num_events__gte=100
)
return CountFilter