1. Luke Plant
  2. django

Commits

jkoc...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed f4d4306

[soc2009/model-validation] Renamed Model's clean method to full_validate to be more consistent with django.forms.

  • Participants
  • Parent commits 6617f5b
  • Branches soc2009/model-validation

Comments (0)

Files changed (7)

File django/db/models/base.py

View file
 
     def validate(self):
         """
-        Hook for doing any extra model-wide validation after Model.clean() been
+        Hook for doing any extra model-wide validation after clean() has been
         called on every field. Any ValidationError raised by this method will
         not be associated with a particular field; it will have a special-case
         association with the field named '__all__'.
                 'field_label': unicode(field_labels)
             }
 
-    def clean(self, exclude=[]):
+    def full_validate(self, exclude=[]):
         """
         Cleans all fields and raises ValidationError containing message_dict of 
         all validation errors if any occur.

File django/forms/models.py

View file
         opts = self._meta
         self.instance = make_instance(self, self.instance, opts.fields, opts.exclude)
         try:
-            self.instance.clean(exclude=self._errors.keys())
+            self.instance.full_validate(exclude=self._errors.keys())
         except ValidationError, e:
             for k, v in e.message_dict.items():
                 if k != NON_FIELD_ERRORS:
             # what about fields that don't validate but aren't present on the form?
             if NON_FIELD_ERRORS in e.message_dict:
                 raise ValidationError(e.message_dict[NON_FIELD_ERRORS])
-            
+
         return self.cleaned_data
 
     def save(self, commit=True):
             fail_message = 'created'
         else:
             fail_message = 'changed'
-
         return save_made_instance(self, self.instance, self._meta.fields, commit, fail_message)
 
     save.alters_data = True

File docs/ref/models/instances.txt

View file
 
 .. versionadded:: 1.2
 
-To validate your model, just call it's ``clean()`` method:
+To validate your model, just call it's ``full_validate()`` method:
 
-.. method:: Model.clean([exclude=[]])
+.. method:: Model.full_validate([exclude=[]])
 
 The optional ``exclude`` argument can contain a list of field names that should
 be omitted when validating. This method raises ``ValidationError`` containing a
 .. method:: Model.validate()
 
 The ``validate()`` method on ``Model`` by default checks for uniqueness of
-fields and group of fields that are declared to be unique so remember to call
-``super.validate()`` if you want this validation to run.
+fields and group of fields that are declared to be unique so, remember to call
+``self.validate_unique()`` or the superclasses ``validate`` method if you want
+this validation to run.
 
 Any ``ValidationError`` raised in this method will be propagated in the
 ``message_dict`` under ``NON_FIELD_ERRORS``.

File tests/modeltests/validation/test_custom_messages.py

View file
 class CustomMessagesTest(ValidationTestCase):
     def test_custom_complex_validator_message(self):
         cmm = CustomMessagesModel(number=42, other=42)
-        self.assertFieldFailsValidationWithMessage(cmm.clean, 'number', ['other != me'])
+        self.assertFieldFailsValidationWithMessage(cmm.full_validate, 'number', ['other != me'])
 
     def test_custom_simple_validator_message(self):
         cmm = CustomMessagesModel(number=12)
-        self.assertFieldFailsValidationWithMessage(cmm.clean, 'number', ['AAARGH'])
+        self.assertFieldFailsValidationWithMessage(cmm.full_validate, 'number', ['AAARGH'])
 
     def test_custom_null_message(self):
         cmm = CustomMessagesModel()
-        self.assertFieldFailsValidationWithMessage(cmm.clean, 'number', ['NULL'])
+        self.assertFieldFailsValidationWithMessage(cmm.full_validate, 'number', ['NULL'])
 

File tests/modeltests/validation/test_unique.py

View file
 
 from django.conf import settings
 from django.db import connection
+from models import CustomPKModel, UniqueTogetherModel, UniqueFieldsModel, UniqueForDateModel, ModelToValidate
 
-from models import CustomPKModel, UniqueTogetherModel, UniqueFieldsModel, UniqueForDateModel, ModelToValidate
 
 class GetUniqueCheckTests(unittest.TestCase):
     def test_unique_fields_get_collected(self):
             ), m._get_unique_checks()
         )
 
-
 class PerformUniqueChecksTest(unittest.TestCase):
     def setUp(self):
         # set debug to True to gain access to connection.queries
         l = len(connection.queries)
         mtv = ModelToValidate(number=10, name='Some Name')
         setattr(mtv, '_adding', True)
-        mtv.clean()
+        mtv.full_validate()
         self.assertEqual(l+1, len(connection.queries))
 
     def test_primary_key_unique_check_not_performed_when_not_adding(self):
         """Regression test for #12132"""
         l = len(connection.queries)
         mtv = ModelToValidate(number=10, name='Some Name')
-        mtv.clean()
+        mtv.full_validate()
         self.assertEqual(l, len(connection.queries))

File tests/modeltests/validation/tests.py

View file
 from test_unique import GetUniqueCheckTests, PerformUniqueChecksTest
 from test_custom_messages import CustomMessagesTest
 
+
 class BaseModelValidationTests(ValidationTestCase):
 
     def test_missing_required_field_raises_error(self):
         mtv = ModelToValidate(f_with_custom_validator=42)
-        self.assertFailsValidation(mtv.clean, ['name', 'number'])
-    
+        self.assertFailsValidation(mtv.full_validate, ['name', 'number'])
+
     def test_with_correct_value_model_validates(self):
         mtv = ModelToValidate(number=10, name='Some Name')
-        self.assertEqual(None, mtv.clean())
+        self.assertEqual(None, mtv.full_validate())
 
     def test_custom_validate_method_is_called(self):
         mtv = ModelToValidate(number=11)
-        self.assertFailsValidation(mtv.clean, [NON_FIELD_ERRORS, 'name'])
+        self.assertFailsValidation(mtv.full_validate, [NON_FIELD_ERRORS, 'name'])
 
     def test_wrong_FK_value_raises_error(self):
         mtv=ModelToValidate(number=10, name='Some Name', parent_id=3)
-        self.assertFailsValidation(mtv.clean, ['parent'])
+        self.assertFailsValidation(mtv.full_validate, ['parent'])
 
-    def test_correct_FK_value_cleans(self):
+    def test_correct_FK_value_validates(self):
         parent = ModelToValidate.objects.create(number=10, name='Some Name')
         mtv=ModelToValidate(number=10, name='Some Name', parent_id=parent.pk)
-        self.assertEqual(None, mtv.clean())
+        self.assertEqual(None, mtv.full_validate())
 
     def test_wrong_email_value_raises_error(self):
         mtv = ModelToValidate(number=10, name='Some Name', email='not-an-email')
-        self.assertFailsValidation(mtv.clean, ['email'])
+        self.assertFailsValidation(mtv.full_validate, ['email'])
 
     def test_correct_email_value_passes(self):
         mtv = ModelToValidate(number=10, name='Some Name', email='valid@email.com')
-        self.assertEqual(None, mtv.clean())
+        self.assertEqual(None, mtv.full_validate())
 
     def test_wrong_url_value_raises_error(self):
         mtv = ModelToValidate(number=10, name='Some Name', url='not a url')
-        self.assertFieldFailsValidationWithMessage(mtv.clean, 'url', [u'Enter a valid value.'])
+        self.assertFieldFailsValidationWithMessage(mtv.full_validate, 'url', [u'Enter a valid value.'])
 
     def test_correct_url_but_nonexisting_gives_404(self):
         mtv = ModelToValidate(number=10, name='Some Name', url='http://google.com/we-love-microsoft.html')
-        self.assertFieldFailsValidationWithMessage(mtv.clean, 'url', [u'This URL appears to be a broken link.'])
+        self.assertFieldFailsValidationWithMessage(mtv.full_validate, 'url', [u'This URL appears to be a broken link.'])
 
     def test_correct_url_value_passes(self):
         mtv = ModelToValidate(number=10, name='Some Name', url='http://www.djangoproject.com/')
-        self.assertEqual(None, mtv.clean()) # This will fail if there's no Internet connection
+        self.assertEqual(None, mtv.full_validate()) # This will fail if there's no Internet connection
 
     def test_text_greater_that_charfields_max_length_eaises_erros(self):
         mtv = ModelToValidate(number=10, name='Some Name'*100)
-        self.assertFailsValidation(mtv.clean, ['name',])
+        self.assertFailsValidation(mtv.full_validate, ['name',])
 

File tests/modeltests/validation/validators.py

View file
 from modeltests.validation import ValidationTestCase
 from models import *
 
+
 class TestModelsWithValidators(ValidationTestCase):
     def test_custom_validator_passes_for_correct_value(self):
         mtv = ModelToValidate(number=10, name='Some Name', f_with_custom_validator=42)
-        self.assertEqual(None, mtv.clean())
+        self.assertEqual(None, mtv.full_validate())
 
     def test_custom_validator_raises_error_for_incorrect_value(self):
         mtv = ModelToValidate(number=10, name='Some Name', f_with_custom_validator=12)
-        self.assertFailsValidation(mtv.clean, ['f_with_custom_validator'])
+        self.assertFailsValidation(mtv.full_validate, ['f_with_custom_validator'])
         self.assertFieldFailsValidationWithMessage(
-                mtv.clean,
+                mtv.full_validate,
                 'f_with_custom_validator',
                 [u'This is not the answer to life, universe and everything!']
             )
 
     def test_custom_complex_validator_raises_error_for_incorrect_value(self):
         mtv = ModelToValidate(number=42, name='Some Name', f_with_custom_validator=42)
-        self.assertFailsValidation(mtv.clean, ['f_with_custom_validator'])
+        self.assertFailsValidation(mtv.full_validate, ['f_with_custom_validator'])
         self.assertFieldFailsValidationWithMessage(
-                mtv.clean,
+                mtv.full_validate,
                 'f_with_custom_validator',
                 [u"Must not equal to 'number''s value"]
             )
 
-
-    def test_complex_validator_isnt_run_if_field_doesnt_clean(self):
+    def test_complex_validator_isnt_run_if_field_doesnt_validate(self):
         mtv = ModelToValidate(number=32, name='Some Name', f_with_custom_validator=32)
         self.assertFieldFailsValidationWithMessage(
-                mtv.clean,
+                mtv.full_validate,
                 'f_with_custom_validator',
                 [u'This is not the answer to life, universe and everything!']
             )