当前位置: 首页>>代码示例>>Python>>正文


Python admin.SimpleListFilter方法代码示例

本文整理汇总了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 
开发者ID:amfoss,项目名称:cms,代码行数:27,代码来源:admin.py

示例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'
        ) 
开发者ID:nesdis,项目名称:djongo,代码行数:21,代码来源:test_checks.py

示例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) 
开发者ID:nesdis,项目名称:djongo,代码行数:17,代码来源:test_checks.py

示例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 
开发者ID:ebmdatalab,项目名称:openprescribing,代码行数:8,代码来源:admin.py

示例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 
开发者ID:GetTogetherComm,项目名称:GetTogether,代码行数:44,代码来源:admin.py


注:本文中的django.contrib.admin.SimpleListFilter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。