Add docs for how to properly work w/ annotations

#10 · open · 1 comments

View on GitHub ↗

rpkilby

Auto generating a filter is tricky, given that annotations are ephemeral. For example, it could not be provided by the queryset instance. The annotation itself could theoretically even change types. Instead, users should declare a filter on the FilterSet class, then override `filter_queryset`, and test if the annotated value is in the form cleaned_data. If so, annotate value and proceed as normal. Something like: ```python class FilterSet(django_filters.FilterSet): annotated = filters.CharFilter() def filter_queryset(self, queryset): data = self.form.cleaned_data if data.get('annotated') is not None: queryset = queryset.annotate(annotated=...) return super().filter_queryset(queryset) ```

Comments

rpkilby

Additionally, they could use a custom filter method and call the annotation method there.