Skip to content

Commit

Permalink
Remove 'Meta.together' option
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan P Kilby committed Oct 19, 2017
1 parent 5691fee commit 01a1f67
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 96 deletions.
36 changes: 1 addition & 35 deletions django_filters/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from .compat import remote_field, remote_queryset
from .conf import settings
from .constants import ALL_FIELDS, EMPTY_VALUES, STRICTNESS
from .constants import ALL_FIELDS, STRICTNESS
from .filters import (
BaseInFilter,
BaseRangeFilter,
Expand All @@ -29,41 +29,13 @@
UUIDFilter
)
from .utils import (
deprecate,
get_all_model_fields,
get_model_field,
resolve_field,
try_dbfield
)


def _together_valid(form, fieldset):
field_presence = [
form.cleaned_data.get(field) not in EMPTY_VALUES
for field in fieldset
]

if any(field_presence):
return all(field_presence)
return True


def get_full_clean_override(together):
# coerce together to list of pairs
if isinstance(together[0], (six.string_types)):
together = [together]

def full_clean(form):
super(form.__class__, form).full_clean()
message = 'Following fields must be together: %s'

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

return full_clean


class FilterSetOptions(object):
def __init__(self, options=None):
self.model = getattr(options, 'model', None)
Expand All @@ -76,10 +48,6 @@ def __init__(self, options=None):

self.form = getattr(options, 'form', forms.Form)

if hasattr(options, 'together'):
deprecate('The `Meta.together` option has been deprecated in favor of overriding `Form.clean`.', 1)
self.together = getattr(options, 'together', None)


class FilterSetMetaclass(type):
def __new__(cls, name, bases, attrs):
Expand Down Expand Up @@ -232,8 +200,6 @@ def form(self):

Form = type(str('%sForm' % self.__class__.__name__),
(self._meta.form,), fields)
if self._meta.together:
Form.full_clean = get_full_clean_override(self._meta.together)
if self.is_bound:
self._form = Form(self.data, prefix=self.form_prefix)
else:
Expand Down
61 changes: 0 additions & 61 deletions tests/test_filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,67 +681,6 @@ class Meta:
self.assertEqual(F(strict=False).strict, STRICTNESS.IGNORE)


class FilterSetTogetherTests(TestCase):

def setUp(self):
self.alex = User.objects.create(username='alex', status=1)
self.jacob = User.objects.create(username='jacob', status=2)
self.qs = User.objects.all().order_by('id')

def test_fields_set(self):
class F(FilterSet):
class Meta:
model = User
fields = ['username', 'status', 'is_active', 'first_name']
together = [
('username', 'status'),
('first_name', 'is_active'),
]
strict = STRICTNESS.RAISE_VALIDATION_ERROR

f = F({}, queryset=self.qs)
self.assertEqual(f.qs.count(), 2)

f = F({'username': 'alex'}, queryset=self.qs)
with self.assertRaises(ValidationError):
f.qs.count()

f = F({'username': 'alex', 'status': 1}, queryset=self.qs)
self.assertEqual(f.qs.count(), 1)
self.assertQuerysetEqual(f.qs, [self.alex.pk], lambda o: o.pk)

def test_single_fields_set(self):
class F(FilterSet):
class Meta:
model = User
fields = ['username', 'status']
together = ['username', 'status']
strict = STRICTNESS.RAISE_VALIDATION_ERROR

f = F({}, queryset=self.qs)
self.assertEqual(f.qs.count(), 2)

f = F({'username': 'alex'}, queryset=self.qs)
with self.assertRaises(ValidationError):
f.qs.count()

f = F({'username': 'alex', 'status': 1}, queryset=self.qs)
self.assertEqual(f.qs.count(), 1)
self.assertQuerysetEqual(f.qs, [self.alex.pk], lambda o: o.pk)

def test_empty_values(self):
class F(FilterSet):
class Meta:
model = User
fields = ['username', 'status']
together = ['username', 'status']

f = F({'username': '', 'status': ''}, queryset=self.qs)
self.assertEqual(f.qs.count(), 2)
f = F({'username': 'alex', 'status': ''}, queryset=self.qs)
self.assertEqual(f.qs.count(), 0)


# test filter.method here, as it depends on its parent FilterSet
class FilterMethodTests(TestCase):

Expand Down

0 comments on commit 01a1f67

Please sign in to comment.