Skip to content

Fix all lookups handling for related fields #128

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

Merged
merged 1 commit into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rest_framework_filters/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.utils import six

from django_filters import filterset, rest_framework
from django_filters.utils import get_model_field

from . import filters
from . import utils
Expand Down Expand Up @@ -77,7 +78,7 @@ def filters_for_model(cls, model, opts):
fields = fields.copy()
for name, lookups in six.iteritems(fields):
if lookups == filters.ALL_LOOKUPS:
field = model._meta.get_field(name)
field = get_model_field(model, name)
fields[name] = utils.lookups_for_field(field)

return filterset.filters_for_model(
Expand Down
11 changes: 11 additions & 0 deletions tests/test_filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ class Meta:
self.assertIsInstance(F.base_filters['author'], filters.ModelChoiceFilter)
self.assertIsInstance(F.base_filters['author__in'], BaseInFilter)

def test_alllookupsfilter_for_related_field(self):
# See: https://github.com/philipn/django-rest-framework-filters/issues/127
class F(FilterSet):
author = filters.AllLookupsFilter(name='author__last_name')

class Meta:
model = Note

self.assertIsInstance(F.base_filters['author'], filters.CharFilter)
self.assertEqual(F.base_filters['author'].name, 'author__last_name')

def test_relatedfilter_combined_with__all__(self):
# ensure that related filter is compatible with __all__ lookups.
class F(FilterSet):
Expand Down