Giter Site home page Giter Site logo

Comments (7)

rpkilby avatar rpkilby commented on May 20, 2024 2

Just to clear up any misconceptions about the various filters and fields:

  • ChoiceFilter/MultipleChoiceFilter work with regular model fields that have choices. The only difference is that the multiple version allows you to filter for multiple values using the color=blue&color=green syntax. The standard ChoiceFilter will chose only one value.
  • AllValuesFilter works identically to ChoiceFilter, except that it pulls its choices from the values stored in the database, not from the field's choices argument. There is no multiple equivalent, although there is an open PR to add it.
  • ModelChoiceFilter and ModelMultipleChoiceFilter are intended to work across relationships. Their choices are pulled from the DB, similar to AllValuesFilter.

The above filters can be created using the declarative syntax. eg,

# model
class MyModel(models.Model):
    color = models.CharField(choices=(
        ('blue', _('blue')),
        ('green', _('green')),
        ... 
    ))
# filterset
class MyFilter(filters.FilterSet):
    color = filters.MultipleChoiceFilter()

    class Meta:
        model = MyModel

There is also a CSV filter base class that is intended to be used as a mixin with more concrete filter types. You could explicitly create this filter, however the Meta.fields syntax will automatically create this for you. eg,

class MyFilter(filters.FilterSet):
    class Meta:
        model = MyModel
        fields = {
            'color': ['exact', 'in', ],
        }

This enables you to filter for color__in=blue,green. I can't remember if I've tested this with reverse relationships, but there isn't any reason it shouldn't work...

from django-rest-framework-filters.

rpkilby avatar rpkilby commented on May 20, 2024

Hi @jsmedmar! Could you provide a more complete example of your filterset and model? There isn't any reason for this to not work.

The multi-value syntax is only supported by the MultipleChoiceFilter and ModelMultipleChoiceFilter. The former needs to be explicitly declared on the filterset, and the latter is generated for reverse foreign relations.

There is also a comma-separated value filter that can be generated for in lookups. eg, color__in=blue,green.

from django-rest-framework-filters.

jsmedmar avatar jsmedmar commented on May 20, 2024

My bad for an incomplete example.

My fields are not MultipleChoiceFields, just regular fields with choices... Therefore I think the MultipleChoiceFilter might not work for me.

Wondering if the color__in=blue,green is usable right away or if I need to add a AllLookupsFilter? Does this work for reverse relationships?

Thank you very much for your quick response

from django-rest-framework-filters.

jsmedmar avatar jsmedmar commented on May 20, 2024

Hello @rpkilby Thank you very much!

I tried:

color = filters.AllLookupsFilter(name="color")

And the color__in syntax works as expected!

from django-rest-framework-filters.

rpkilby avatar rpkilby commented on May 20, 2024

Are you sure? The lhs (color) corresponds with the query name. If this were your only filter, no filtering would happen.

from django-rest-framework-filters.

Dragonwarrior47 avatar Dragonwarrior47 commented on May 20, 2024

how can a define all lookup filter on reverse relation.
I have a model A, and model B
Model B has foreign key to A.

Now I want to have a all lookup filter in Filter A and want to look for the field which has a foreign key to A in B

from django-rest-framework-filters.

rpkilby avatar rpkilby commented on May 20, 2024

Hi @Dragonwarrior47 - please don't ask unrelated questions in old issues. If you're have a new issue, please open a new issue. Also, it would be helpful to have a more concrete code example. It's not entirely clear what you're looking for.

from django-rest-framework-filters.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.