Skip to content

Commit

Permalink
Remove invalid field data when 'together' fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan P Kilby committed Oct 12, 2017
1 parent 157e08a commit 1b3bcf7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion django_filters/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def full_clean(form):

for each in together:
if not _together_valid(form, each):
return form.add_error(None, message % ','.join(each))
form.add_error(None, message % ','.join(each))
for field_name in each:
form.cleaned_data.pop(field_name, None)

return full_clean

Expand Down
7 changes: 6 additions & 1 deletion tests/test_filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,10 +690,15 @@ class Meta:
fields = ['username', 'status']
together = ['username', 'status']

# valid - neither field present, no filtering
f = F({'username': '', 'status': ''}, queryset=self.qs)
self.assertTrue(f.is_valid())
self.assertEqual(f.qs.count(), 2)

# invalid - both fields not present, no filtering
f = F({'username': 'alex', 'status': ''}, queryset=self.qs)
self.assertEqual(f.qs.count(), 0)
self.assertFalse(f.is_valid())
self.assertEqual(f.qs.count(), 2)


# test filter.method here, as it depends on its parent FilterSet
Expand Down

0 comments on commit 1b3bcf7

Please sign in to comment.