Open ahmohamed opened 7 years ago
Thanks for the suggestion. Reason why it checks agains a model is because you are using a ModelFilterSet
which gets the fields from the model definition in order to get the form class for the filter to use for data validation. Will try to implement in future however not sure how that will work since annotated fields might be loosing their data-type so I would not know which form class to use for those fields.
Meanwhile you should be able to manually define a filterset for that field. Something like:
class MyFilterSet(ModelFilterSet):
article_count = Filter(form_class=forms.IntegerField())
class Meta(object):
model = Reporter
you can always see what filters are set on the filterset by doing a representation of the filterset:
print(repr(MyFilterSet()))
Thank you for the quick response! The work around is working very fine for now.
I am not sure how complex to implement this, but it would be really useful if we can filter on aggregate (annotated) fields.
Exmaple: Consider these models
And the DRF viewset
We filter reporters that have at least n articles by
reporter?article_count__gte=5
.This setup currently result in error
Reporter has no field named 'article_count'
. Since the filtering is done on the queryset not the model, I am not sure why the model is being checked.Thanks.