I want to make the default behaviour of my CharFilters that they have the accents removed for comparison. How can I do that besides manually applying a method to each one?
title__icontains = CharFilter(field_name='title', method='unaccentFilter')
publishers__name = CharFilter(field_name='publishers__name', method='unaccentFilter')
developers__name = CharFilter(field_name='developers__name', method='unaccentFilter')
platforms__name = CharFilter(field_name='platforms__name', method='unaccentFilter')
genres__name = CharFilter(field_name='genres__name', method='unaccentFilter')
engine__name = CharFilter(field_name='engine__name', method='unaccentFilter')
websites__category = ChoiceFilter(choices=STATUS_CHOICES)
similar_games__title = CharFilter(field_name='similar_games__title', method='unaccentFilter')
class Meta:
model = Game
fields = {
'title': ['iexact'],
'summary': ['icontains'],
'release_date': ['iexact', 'range', 'gte', 'lte'],
'category': ['iexact', 'icontains'],
}
def unaccentFilter(self, queryset, name, value):
normalized_value = unidecode.unidecode(value)
return queryset.annotate(unaccented=Unaccent(name)) \
.filter(unaccented__icontains=normalized_value)