Comments (9)
Hi @jackdbernier. If this is a one off problem, I would try something like the following:
class SomeFilter(filters.FilterSet):
# Notice the double underscores separating `a_field` and `in`
a_field__in = filters.InSetCharFilter(name='a_field', lookup_type='in', choices=choices)
class Meta:
model = SomeModel
# I recommend explicitly declaring relevant lookups, instead of using AllLookupsFilter.
fields = {
'a_field': ['exact', 'gt', 'gte', 'lt', 'lte', 'etc...'],
}
If this is a common issue for you, you could also override the behavior of fix_filter_field
by subclassing FilterSet
.
from django-rest-framework-filters.
btw - I'm currently working on a patch for django-filter that should negate the need for a fix in drf-filters (fix_filter_field would no longer be necessary).
from django-rest-framework-filters.
Hi @jackdbernier, django-filter v0.13.0 now supports in
& range
csv lookups, as well as date transforms. The upcoming release of drf-filters (v0.8.0) should be released soon, and no longer relies on fix_filter_field
.
from django-rest-framework-filters.
Great I'll give a try at the new version. Thanks.
from django-rest-framework-filters.
As a follow up to this here's one way of solving the problem:
import rest_framework_filters as filters
from models import Foo
class FooFilter(filters.FilterSet):
class Meta:
model = Foo
fields = {
'bar': ['exact', 'in'],
}
Then you can use bar__in=baz,zulu
as well as bar__in!=baz,zulu
as expected.
I did need to use the fields dictionary though to explicitly provide the lookup types. Perhaps that means some follow-up work would just solve this if the "default all" approach used by django-rest-framework-filters included in
?
from django-rest-framework-filters.
@evenicoulddoit - AllLookupsFilter should include in
filtering. What version of drf-filters are you using?
from django-rest-framework-filters.
@rpkilby djangorestframework-filters==0.8.0
from django-rest-framework-filters.
Not sure if this should be in the same issue or a separate one, but I can confirm just running into this as well. Using 0.8.0
, it doesn't appear that in
is included in AllLookupsFilter
.
Rather, when I send up ?id__in=2,3,4,5,6
, for example, I weirdly only get returned the entity with id 6
.
I'm running into this in my test suite when using
self.client.get(self.api_url, data={'id__in': [2, 3, 4, 5, 6]})
class DeviceFilter(FilterSet):
organization = RelatedFilter(organization_filter, name='organization')
project = RelatedFilter(project_filter, name='project')
id = AllLookupsFilter(name='id')
created = AllLookupsFilter(name='created')
updated = AllLookupsFilter(name='updated')
name = AllLookupsFilter(name='name')
class Meta:
model = Device
from django-rest-framework-filters.
Actually ignore that, this was an error on my part. Passing the list in as a string 2,3,4,5,6
fixes everything for me.
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.