本文整理匯總了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