Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for getting help_text from model fields #754

Closed
jter opened this issue Aug 2, 2017 · 2 comments
Closed

Proposal for getting help_text from model fields #754

jter opened this issue Aug 2, 2017 · 2 comments

Comments

@jter
Copy link

jter commented Aug 2, 2017

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?

@rpkilby
Copy link
Collaborator

rpkilby commented Aug 8, 2017

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_texts is a little less useful.

  1. There may be several filters for the same model field, making the help_text repetitive/redundant.
  2. The data type of the filter doesn't necessarily match the data type of the model field. eg, information on entering a datetime may not be appropriate for an 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

@jter
Copy link
Author

jter commented Sep 19, 2017

Thank you for your illustrating, clear and useful.
@rpkilby

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants