Skip to content

Commit 5a98c8f

Browse files
author
Ryan P Kilby
committed
Fix all lookups handling for related fields (#128)
1 parent 0a95a65 commit 5a98c8f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

rest_framework_filters/filterset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from django.utils import six
1010

1111
from django_filters import filterset, rest_framework
12+
from django_filters.utils import get_model_field
1213

1314
from . import filters
1415
from . import utils
@@ -139,7 +140,7 @@ def filters_for_model(cls, model, opts):
139140
lookups = '__all__'
140141

141142
if lookups == '__all__':
142-
field = model._meta.get_field(name)
143+
field = get_model_field(model, name)
143144
fields[name] = utils.lookups_for_field(field)
144145

145146
return filterset.filters_for_model(

tests/test_filterset.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ class Meta:
6363
self.assertIsInstance(F.base_filters['author'], filters.ModelChoiceFilter)
6464
self.assertIsInstance(F.base_filters['author__in'], BaseInFilter)
6565

66+
def test_alllookupsfilter_for_related_field(self):
67+
# See: https://github.com/philipn/django-rest-framework-filters/issues/127
68+
class F(FilterSet):
69+
author = filters.AllLookupsFilter(name='author__last_name')
70+
71+
class Meta:
72+
model = Note
73+
74+
self.assertIsInstance(F.base_filters['author'], filters.CharFilter)
75+
self.assertEqual(F.base_filters['author'].name, 'author__last_name')
76+
6677
def test_relatedfilter_combined_with__all__(self):
6778
# ensure that related filter is compatible with __all__ lookups.
6879
class F(FilterSet):

0 commit comments

Comments
 (0)