Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #20594 -- Add validation to models.SlugField.

Thanks carbonXT for the report.
  • Loading branch information...
commit dc9c359546580d40df209816cef244b78dcf7435 1 parent 675558d
Baptiste Mispelon authored June 13, 2013 timgraham committed June 13, 2013
3  django/db/models/fields/__init__.py
@@ -1211,6 +1211,7 @@ def formfield(self, **kwargs):
1211 1211
         return super(PositiveSmallIntegerField, self).formfield(**defaults)
1212 1212
 
1213 1213
 class SlugField(CharField):
  1214
+    default_validators = [validators.validate_slug]
1214 1215
     description = _("Slug (up to %(max_length)s)")
1215 1216
 
1216 1217
     def __init__(self, *args, **kwargs):
@@ -1320,12 +1321,12 @@ def formfield(self, **kwargs):
1320 1321
         return super(TimeField, self).formfield(**defaults)
1321 1322
 
1322 1323
 class URLField(CharField):
  1324
+    default_validators = [validators.URLValidator()]
1323 1325
     description = _("URL")
1324 1326
 
1325 1327
     def __init__(self, verbose_name=None, name=None, **kwargs):
1326 1328
         kwargs['max_length'] = kwargs.get('max_length', 200)
1327 1329
         CharField.__init__(self, verbose_name, name, **kwargs)
1328  
-        self.validators.append(validators.URLValidator())
1329 1330
 
1330 1331
     def formfield(self, **kwargs):
1331 1332
         # As with CharField, this will cause URL validation to be performed
5  django/forms/fields.py
@@ -638,10 +638,7 @@ class URLField(CharField):
638 638
     default_error_messages = {
639 639
         'invalid': _('Enter a valid URL.'),
640 640
     }
641  
-
642  
-    def __init__(self, max_length=None, min_length=None, *args, **kwargs):
643  
-        super(URLField, self).__init__(max_length, min_length, *args, **kwargs)
644  
-        self.validators.append(validators.URLValidator())
  641
+    default_validators = [validators.URLValidator()]
645 642
 
646 643
     def to_python(self, value):
647 644
 
1  tests/validation/models.py
@@ -19,6 +19,7 @@ class ModelToValidate(models.Model):
19 19
     email = models.EmailField(blank=True)
20 20
     url = models.URLField(blank=True)
21 21
     f_with_custom_validator = models.IntegerField(blank=True, null=True, validators=[validate_answer_to_universe])
  22
+    slug = models.SlugField(blank=True)
22 23
 
23 24
     def clean(self):
24 25
         super(ModelToValidate, self).clean()
6  tests/validation/tests.py
@@ -53,7 +53,11 @@ def test_wrong_url_value_raises_error(self):
53 53
 
54 54
     def test_text_greater_that_charfields_max_length_raises_erros(self):
55 55
         mtv = ModelToValidate(number=10, name='Some Name'*100)
56  
-        self.assertFailsValidation(mtv.full_clean, ['name',])
  56
+        self.assertFailsValidation(mtv.full_clean, ['name'])
  57
+
  58
+    def test_malformed_slug_raises_error(self):
  59
+        mtv = ModelToValidate(number=10, name='Some Name', slug='##invalid##')
  60
+        self.assertFailsValidation(mtv.full_clean, ['slug'])
57 61
 
58 62
 
59 63
 class ArticleForm(forms.ModelForm):

0 notes on commit dc9c359

Please sign in to comment.
Something went wrong with that request. Please try again.