Skip to content

Commit fe09926

Browse files
author
Ryan P Kilby
committed
Ensure RelatedFilter nested field behavior
1 parent ab4c763 commit fe09926

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

tests/test_filtering.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django_filters import FilterSet as DFFilterSet
1010

1111
from .testapp.models import (
12-
User, Note, Post, Cover, A, B, C, Person, Tag, BlogPost,
12+
User, Note, Post, Cover, Page, A, B, C, Person, Tag, BlogPost,
1313
)
1414

1515
from .testapp.filters import (
@@ -18,6 +18,7 @@
1818
PostFilter,
1919
BlogPostFilter,
2020
CoverFilterWithRelated,
21+
PageFilterWithAliasedNestedRelated,
2122
NoteFilterWithAll,
2223
NoteFilterWithRelated,
2324
NoteFilterWithRelatedDifferentName,
@@ -110,11 +111,13 @@ def setUpTestData(cls):
110111
Cover.objects.create(post=post1, comment="Cover 1")
111112
Cover.objects.create(post=post3, comment="Cover 2")
112113

113-
# #######################
114-
# # Create pages
115-
# #######################
116-
# page1 = Page.objects.create(title="First page", content="First first.")
117-
# Page.objects.create(title="Second page", content="Second second.", previous_page=page1)
114+
#######################
115+
# Create pages
116+
#######################
117+
Page.objects.create(title="First page", content="First first.")
118+
Page.objects.create(title="Second page", content="Second second.", previous_page_id=1)
119+
Page.objects.create(title="Third page", content="Third third.", previous_page_id=2)
120+
Page.objects.create(title="Fourth page", content="Fourth fourth.", previous_page_id=3)
118121

119122
################################
120123
# ManyToMany
@@ -220,6 +223,19 @@ def test_relatedfilter_for_related_alllookups_and_different_filter_name(self):
220223
f = NoteFilterWithRelatedAllDifferentFilterName(GET, queryset=Note.objects.all())
221224
self.assertEqual(len(list(f.qs)), 4)
222225

226+
def test_relatedfilter_for_aliased_nested_relationships(self):
227+
qs = Page.objects.order_by('pk')
228+
229+
f1 = PageFilterWithAliasedNestedRelated({'two_pages_back': '1'}, queryset=qs)
230+
f2 = PageFilterWithAliasedNestedRelated({'two_pages_back': '2'}, queryset=qs)
231+
f3 = PageFilterWithAliasedNestedRelated({'two_pages_back': '3'}, queryset=qs)
232+
f4 = PageFilterWithAliasedNestedRelated({'two_pages_back': '4'}, queryset=qs)
233+
234+
self.assertQuerysetEqual(f1.qs, [3], lambda p: p.pk)
235+
self.assertQuerysetEqual(f2.qs, [4], lambda p: p.pk)
236+
self.assertQuerysetEqual(f3.qs, [], lambda p: p.pk)
237+
self.assertQuerysetEqual(f4.qs, [], lambda p: p.pk)
238+
223239
def test_relatedfilter_different_name(self):
224240
# Test the name filter on the related UserFilter set.
225241
GET = {

tests/testapp/filters.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ class Meta:
121121
fields = []
122122

123123

124+
class PageFilterWithAliasedNestedRelated(FilterSet):
125+
title = filters.CharFilter(name='title')
126+
two_pages_back = RelatedFilter(PostFilter, name='previous_page__previous_page', queryset=Page.objects.all())
127+
128+
class Meta:
129+
model = Page
130+
fields = []
131+
132+
124133
class TagFilter(FilterSet):
125134
name = AllLookupsFilter(name='name')
126135

0 commit comments

Comments
 (0)