-
Notifications
You must be signed in to change notification settings - Fork 754
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
Automatic __isnull filter field does not work in django-rest-framework usage #743
Comments
Hi @sarvi. My guess is that your settings are using the old filter backend that is provided by DRF. This has been deprecated in favor of the backend shipped with django-filter. The correct path is |
This is from my settings file REST FRAMEWORK Configuration SettingsREST_FRAMEWORK = { It looks the same as you suggest |
Oh, the problem is that the list syntax for filter_fields = {
'mirrorof': ['isnull'],
...
} I wrote a brief test just to check, and I'm surprised that you don't get an exception (or maybe it's just hidden by the client code?) You should see something like... Traceback (most recent call last):
File "./django-filter/tests/rest_framework/test_backends.py", line 215, in test_fields_with_lookup_exprs
filter_class = backend.get_filter_class(view, view.get_queryset())
File "./django-filter/django_filters/rest_framework/backends.py", line 40, in get_filter_class
class AutoFilterSet(self.default_filter_set):
File "./django-filter/django_filters/filterset.py", line 86, in __new__
new_class.base_filters = new_class.get_filters()
File "./django-filter/django_filters/filterset.py", line 313, in get_filters
"%s" % ', '.join(undefined)
TypeError: 'Meta.fields' contains fields that are not defined on this FilterSet: mirrorof__isnull |
Hi @sarvi - any followup here? Were your issues resolved? |
The way I resolve the issue was to do this
I was expecting the following to work
The problem seems to be that mirrorof__isnull gets mapped to BooleanFilter from django_filter instead of django_filter.rest_framework I have it working. But I think this is a bug |
Hi @sarvi - what happens when you do the following? from django_filters import rest_framework as filters
class SnapshotFilter(filters.FilterSet):
class Meta:
model = Snapshot
fields = ('mirrorof__isnull', ) You should get a |
Hi @rpkilby But I wasn't trying that.
I do see the following warning, but can't tell what that warning means.
|
If you look at the underlying code, the example is essentially the same as setting class SnapshotViewSet(views.ModelViewSet):
filter_fields = ('mirrorof__isnull', )
... The schema generation compatibility warning is an artifact of the underlying |
yes. I am using coreapi to fetch the data. Ok I sorta get it now. Looks like coreapi is some home masking the original error. Is there a better way for me to set the environment such that I am able to see the original TypeError? I think this issue can be closed. |
This is a breakdown of what's happening:
I don't think so - the warning just needs to be updated to include the original exception's text. |
I was having a very similar problem. Im using filtering, searching and sorting but trying to filter on __isnull would not work. I tried what @sarvi did and was able to get it working.
|
Hi @yaplej. Most likely,
@sarvi - yep. I've opened a separate issue to specifically address the warning's message. |
Hello @rpkilby honestly I'm just fumbling though this right now learning as I go. I entirely expected I was doing something wrong but excited someone else bumped into it and had a solution.
and my Model
|
My only guess is that either your Otherwise, this is difficult to debug without a meaningful stack trace. If there is an issue with |
filter_fields=('seq_annotations__isnull',) is desirable but it can't. See more detail: carltongibson/django-filter#743
The documentation says that defining __isnull in the filter_fields for a django-rest-framework viewset should allow for filtering based on __isnull.
Didnt work for me.
I have the following code on the server side
from django_filters import rest_framework as filters
class SnapshotViewSet(views.ModelViewSet):
serializer_class = volserializers.SnapshotSerializer
filter_fields = ('snapshot_name', 'branch__branch_name',
'branch__id', 'branch__team__group__name', 'volume__id', 'mirrorof__isnull')
The following code on the client side
result = self.client_action('api/wit/snapshot/list'), {'page': 1, 'ordering': 'id', 'mirrorof__isnull': True})
always returned records with and without mirror=None
I root caused it to the autofield __isnull doesnt seem to be using
django_filters.BooleanField
instead of
django_filters.rest_framework.filters.BooleanField
I fixed by not using the auto filter field, instead defining
class SnapshotFilter(filters.FilterSet):
mirrorof__isnull = filters.BooleanFilter(name='mirrorof', lookup_expr='isnull')
class Meta:
model = Snapshot
fields = ('snapshot_name', 'branch__branch_name',
'branch__id', 'branch__team__group__name', 'volume__id', 'mirrorof__isnull',
'mirrorof__id', 'mirrorof__mirrorof__id'
)
The key being the filters is from django_filters.rest_framework instead of django_filters.
Not sure if this is an expected behaviour, thought i'd report it considering it wasnt clear from the documentation and took a while to figure out.
The text was updated successfully, but these errors were encountered: