Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class EmailDuplicatesFilter(SimpleListFilter):
- """
- Return user duplicates by email
- """
- title = _("Duplicates")
- parameter_name = 'duplicate_field'
- def lookups(self, request, model_admin):
- return (
- ('email', _('Email')),
- )
- def queryset(self, request, queryset):
- if self.value() == 'email':
- dups = (
- queryset
- .annotate(lower_email=Lower('email'))
- .values('lower_email')
- .annotate(count=Count('lower_email'))
- .values('lower_email', 'count')
- .filter(count__gt=1)
- .order_by('lower_email', 'count')
- )
- return (
- queryset
- .annotate(lower_email=Lower('email'))
- .filter(lower_email__in=[item['lower_email'] for item in dups])
- .order_by('lower_email')
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement