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

Filtering ArrayField #473

Closed
sassanh opened this issue Aug 24, 2016 · 4 comments
Closed

Filtering ArrayField #473

sassanh opened this issue Aug 24, 2016 · 4 comments

Comments

@sassanh
Copy link

sassanh commented Aug 24, 2016

Is it possible to filter fields defined in django.contrib.postgres.fields like ArrayField?

@rpkilby
Copy link
Collaborator

rpkilby commented Aug 24, 2016

django-filter currently doesn't have any filters compatible with the postgres contrib fields - you would need to create your own. It will be more or less difficult depending on what you're trying to do. Supporting the custom lookups for ArrayField would be fairly trivial:

class CharArrayFilter(filters.BaseCSVFilter, filters.CharFilter):
     pass

class MyFilterSet(filters.FilterSet):
    tags__contains = CharArrayFilter(name='tags', lookup_expr='contains')

    class Meta:
        model = SomethingTaggable
GET http://localhost/api/something-taggable?tags__contains=a,b,c

The above filter will validate the comma-separated inputs and return a list of cleaned values, which is suitable for the underlying .filter() call.

On the other hand, it's a little more complicated to handle the index and slice transforms, which are unstructured and have a theoretically infinite amount of combinations.

@sassanh
Copy link
Author

sassanh commented Aug 24, 2016

Thanks for info.

@carltongibson
Copy link
Owner

I'm going to close this as Out of Scope for the moment. Happy to consider documented, tested pull requests. We may have capacity to reconsider in the future.

@Is0MerLNtaLt
Copy link

Update:
Parameter name has been renamed to field_name so the MyFilterSet should look like:

class MyFilterSet(filters.FilterSet):
    tags__contains = CharArrayFilter(field_name='tags', lookup_expr='contains')

    class Meta:
        model = SomethingTaggable

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

4 participants