@@ -98,7 +98,7 @@ def setUpTestData(cls):
9898 ########################################################################
9999 # Create posts #########################################################
100100 post1 = Post .objects .create (note = note1 , content = "Test content in post 1" )
101- Post .objects .create (note = note2 , content = "Test content in post 2" )
101+ post2 = Post .objects .create (note = note2 , content = "Test content in post 2" )
102102 post3 = Post .objects .create (note = note4 , content = "Test content in post 3" )
103103
104104 ########################################################################
@@ -122,6 +122,13 @@ def setUpTestData(cls):
122122 post1 .tags .set ([t1 , t3 ])
123123 post3 .tags .set ([t3 ])
124124
125+ ########################################################################
126+ # ManyToMany distinct ##################################################
127+ t4 = Tag .objects .create (name = "test1" )
128+ t5 = Tag .objects .create (name = "test2" )
129+
130+ post2 .tags .set ([t4 , t5 ])
131+
125132 ########################################################################
126133 # Recursive relations ##################################################
127134 a = A .objects .create (title = "A1" )
@@ -299,6 +306,15 @@ def test_m2m_relation(self):
299306 contents = set ([post .content for post in f .qs ])
300307 self .assertEqual (contents , {'Test content in post 1' , 'Test content in post 3' })
301308
309+ def test_m2m_distinct (self ):
310+ GET = {
311+ 'tags__name__startswith' : 'test' ,
312+ }
313+ f = PostFilter (GET , queryset = Post .objects .all ())
314+ self .assertEqual (len (list (f .qs )), 1 )
315+ contents = set ([post .content for post in f .qs ])
316+ self .assertEqual (contents , {'Test content in post 2' })
317+
302318 def test_nonexistent_related_field (self ):
303319 """
304320 Invalid filter keys (including those on related filters) are invalid
0 commit comments