Specify in which order to apply filters in django_filters.FilterSet

Within a django_filters.FilterSet:

class SomeFilter(django_filters.FilterSet):
    model = SomeModel
    fields = {
        "is_archived": ("exact",),
    }

    include_ancestors = django_filters.BooleanFilter(method="include_ancestors_filter")

    def include_ancestors_filter(self, queryset, name, value):
        pass

how can I specify that the filter field include_ancestors should be applied after all other filter fields (e.g. is_archived)?

This is needed in my case because including the ancestors depends on the result set (i.e. depends on what children have been included based on all other filters).

You can probably "hack" this into it by moving the include_ancestors down the list of values:

class MoveDownForm:
  move_down_field = 'include_ancestors'
  
  @property
  def cleaned_data(self):
    new = old = super().cleaned_data
    if self.move_down_field in old:
      new = {k: v for k, v in old.items() if k != self.move_down_field}
      new['move_down_field'] = old['move_down_field']
    return new

and then work with:

class SomeFilter(django_filters.FilterSet):
    class Meta:
     model = SomeModel
     form = MoveDownForm
     fields = {
        "is_archived": ("exact",),
     }

    include_ancestors = django_filters.BooleanFilter(method="include_ancestors_filter")

    def include_ancestors_filter(self, queryset, name, value):
        pass

But I would test this with two custom filters and check the order of printing if you swap the querystring parameters.

Вернуться на верх