Comments (7)
Just to clear up any misconceptions about the various filters and fields:
ChoiceFilter
/MultipleChoiceFilter
work with regular model fields that havechoices
. The only difference is that themultiple
version allows you to filter for multiple values using thecolor=blue&color=green
syntax. The standardChoiceFilter
will chose only one value.AllValuesFilter
works identically toChoiceFilter
, except that it pulls its choices from the values stored in the database, not from the field'schoices
argument. There is no multiple equivalent, although there is an open PR to add it.ModelChoiceFilter
andModelMultipleChoiceFilter
are intended to work across relationships. Their choices are pulled from the DB, similar toAllValuesFilter
.
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.
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.
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.
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.
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.
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.
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)
- Django 3 support HOT 1
- RecursionError for circular self relationship HOT 1
- filter on two model HOT 1
- Trying out ComplexJSONFilterBackend HOT 1
- ImportError: cannot import name 'six' from 'django.utils' HOT 1
- Specify Model Dynamiclly HOT 2
- TypeError: 'Meta.fields' must not contain non-model field names HOT 2
- Is this project still alive? HOT 3
- CI status failures HOT 1
- Invalid filter for properties of model of RelatedMultipleFilter HOT 1
- Ordering (sorting) from URL in ComplexFilterBackend HOT 1
- Combining filters for multi-valued relationships with `AND` HOT 2
- RelatedMultipleFilter doesn't work as expected HOT 1
- django-rest-framework-filters does not seem to support RelatedFilters with non-default relationships
- PROPOSAL: Custom help text for FilterSet
- ImportError: cannot import name 'QUERY_TERMS' from 'django.db.models.sql.constants'
- Django 4.x compatibility HOT 5
- Consider transferring ownership to Jazzband HOT 2
- How to use the field 'in' in django-rest-framework-filters, like 'user_id__in=[12,13]'? HOT 2
- Tests fail with Django 4.2: ImportError: cannot import name 'url' from 'django.conf.urls'
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-rest-framework-filters.