How to rename (exposed in API) filter field name using django-filters?
You need to provide model field as name in django_filters with field type. I am considering you are trying to filter by championship id.
class MatchFilterSet(FilterSet):
championship = django_filters.NumberFilter(field_name='group__championship_id')
class Meta:
model = Match
fields = ['championship']
After Naresh response I have figured out the source of error.
It was the implementation of the model's view:
class MatchViewSet(ModelViewSet):
filter = MatchFilterSet
(...)
For django-filter
it should be filter_class
rather than filter
, so the correct implementation is:
class MatchViewSet(ModelViewSet):
filter_class = MatchFilterSet
(...)
Also, I've changed the implementation of the model's filter to be more like Naresh suggested:
class MatchFilterSet(FilterSet):
championship = NumberFilter(field_name='group__championship')
class Meta:
model = Match
fields = ['championship']
The difference between above and the Naresh's one is the luck of _id
part which is not necessary.
After these changes everything works fine.