It would be great that filters of FilterSet
could get help_text
from fields of Models. This feature can save some repetitive work when we need help_text
for auto-generated docs from model fields without redefining the filters in FilterSet
. Could this proposal be a good idea?
Hi @jter. I feel like this has been talked about before, but I can't find the relevant discussion. In short, this suggestion works well for FilterSets like...
class UserFilter(FilterSet):
class Meta:
model = User
fields = ['username', 'email', 'name', ...]
Adding the help_text
is reasonable in this case, since the filter's data type matches the data type of the underlying model field. eg /api/users?username=jter
However, Meta.fields
can also be used like so:
class ArticleFilter(FilterSet):
class Meta:
model = Article
fields = {
'author': ['exact', 'startswith', 'endswith', ...],
'title': ['exact', 'startswith', 'endswith', ...],
'published_at': ['exact', 'isnull'],
}
In this case, adding in the model's help_text
s is a little less useful.
help_text
repetitive/redundant.isnull
lookup where the input is a boolean. This would be fairly straightforward to achieve with a custom FilterSet base class:
class HelpfulFilterSet(django_filters.FilterSet):
@classmethod
def filter_for_field(cls, f, name, lookup_expr):
filter = super(HelpfulFilterSet, cls).filter_for_field(f, name, lookup_expr)
filter.extra['help_text'] = f.help_text
return filter
Thank you for your illustrating, clear and useful.
@rpkilby
Most helpful comment
Hi @jter. I feel like this has been talked about before, but I can't find the relevant discussion. In short, this suggestion works well for FilterSets like...
Adding the
help_text
is reasonable in this case, since the filter's data type matches the data type of the underlying model field. eg/api/users?username=jter
However,
Meta.fields
can also be used like so:In this case, adding in the model's
help_text
s is a little less useful.help_text
repetitive/redundant.isnull
lookup where the input is a boolean.This would be fairly straightforward to achieve with a custom FilterSet base class: