From 01a1f67e78bb61b02a07cad361bbfe7a26ba2757 Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Wed, 18 Oct 2017 03:27:45 -0400 Subject: [PATCH] Remove 'Meta.together' option --- django_filters/filterset.py | 36 +--------------------- tests/test_filterset.py | 61 ------------------------------------- 2 files changed, 1 insertion(+), 96 deletions(-) diff --git a/django_filters/filterset.py b/django_filters/filterset.py index 49f8f52fe..da100952e 100644 --- a/django_filters/filterset.py +++ b/django_filters/filterset.py @@ -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, @@ -29,7 +29,6 @@ UUIDFilter ) from .utils import ( - deprecate, get_all_model_fields, get_model_field, resolve_field, @@ -37,33 +36,6 @@ ) -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) @@ -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): @@ -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: diff --git a/tests/test_filterset.py b/tests/test_filterset.py index 045eaed69..976a205a9 100644 --- a/tests/test_filterset.py +++ b/tests/test_filterset.py @@ -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):