Answer the question
In order to leave comments, you need to log in
How to override multiplechoicefilter so that it filters not only by all selected values, but also by an additional parameter?
I'm using djano-filter to filter on specific fields. My model has a region field which I filter using multiplechoicefilter. Everything works correctly, if I select several areas, then everything works correctly.
But I need to expand the operation of this method so that it searches not only by value, but also so that if the database contains the word "any" instead of a certain region, then it takes this into account. That is, if a person selects the "Moscow" district, then he will display records where both "Moscow" and "any" are written. I did not find in the documentation how this can be done with this filter.
I tried to set my own method , but without success. When I had just choicefilter , instead of multiple , everything worked out. Here I don't know how to do it.
class DailyOrdersFilter(django_filters.FilterSet):
STATUS_CHOICES4 = (
('Советский', 'Советский'),
('Приволжский', 'Приволжский'),
district_kazan = django_filters.MultipleChoiceFilter(field_name='district', choices=STATUS_CHOICES4,
label='Требуемый район', lookup_expr='icontains')
def get_filter_predicate(self, v):
name = self.field_name
if name and self.lookup_expr != settings.DEFAULT_LOOKUP_EXPR:
name = LOOKUP_SEP.join([name, self.lookup_expr])
try:
return {name: getattr(v, self.field.to_field_name)}
except (AttributeError, TypeError):
return {name: v}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question