Last active
March 19, 2017 13:25
-
-
Save kholidfu/9b596fd11cffbd4355ed30a5a390e00f to your computer and use it in GitHub Desktop.
custom_filter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NumberOfImagesFilter(admin.SimpleListFilter): | |
"""Filter based on numbers of images. | |
== 7 | |
> 6 | |
< 7 | |
""" | |
title = _('Number of Images') | |
parameter_name = 'imgcount' | |
def lookups(self, request, model_admin): | |
return ( | |
('7', _('7')), | |
('lebihdari7', _('Lebih dari 7')), | |
('kurangdari7', _('Kurang dari 7')), | |
) | |
def queryset(self, request, queryset): | |
if self.value() == '7': | |
return queryset.annotate(num_images=Count('image')).filter(num_images=7) | |
elif self.value() == 'lebihdari7': | |
return queryset.annotate(num_images=Count('image')).filter(num_images__gt=7) | |
elif self.value() == 'kurangdari7': | |
return queryset.annotate(num_images=Count('image')).filter(num_images__lt=7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment