Python django.contrib.admin.SimpleListFilter() Examples

The following are 7 code examples of django.contrib.admin.SimpleListFilter(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.contrib.admin , or try the search function .
Example #1
Source File: admin.py    From cms with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #2
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
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'
        ) 
Example #3
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
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'
        ) 
Example #4
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
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) 
Example #5
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
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) 
Example #6
Source File: admin.py    From openprescribing with MIT License 5 votes vote down vote up
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 
Example #7
Source File: admin.py    From GetTogether with BSD 2-Clause "Simplified" License 4 votes vote down vote up
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