Skip to content

Commit

Permalink
Fixed #8040 -- SlugField now returns a proper formfield to deal with …
Browse files Browse the repository at this point in the history
…validation. Thanks Daniel Pope for the ticket and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
brosner committed Aug 23, 2008
1 parent a64dc39 commit 5061970
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
7 changes: 7 additions & 0 deletions django/db/models/fields/__init__.py
Expand Up @@ -890,6 +890,13 @@ def __init__(self, *args, **kwargs):
def get_internal_type(self):
return "SlugField"

def formfield(self, **kwargs):
defaults = {'form_class': forms.RegexField, 'regex': r'^[a-zA-Z0-9_-]+$',
'error_messages': {'invalid': _(u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.")},
}
defaults.update(kwargs)
return super(SlugField, self).formfield(**defaults)

class SmallIntegerField(IntegerField):
def get_manipulator_field_objs(self):
return [oldforms.SmallIntegerField]
Expand Down
3 changes: 2 additions & 1 deletion docs/modelforms.txt
Expand Up @@ -65,7 +65,8 @@ the full list of conversions:
(from ``django.contrib.localflavor.us``)
``PositiveIntegerField`` ``IntegerField``
``PositiveSmallIntegerField`` ``IntegerField``
``SlugField`` ``CharField``
``SlugField`` ``RegexField`` accepting only letters,
numbers, underscores and hyphens
``SmallIntegerField`` ``IntegerField``
``TextField`` ``CharField`` with ``widget=Textarea``
``TimeField`` ``TimeField``
Expand Down
4 changes: 2 additions & 2 deletions tests/modeltests/model_forms/models.py
Expand Up @@ -301,11 +301,11 @@ def __unicode__(self):
[<Category: Entertainment>, <Category: It's a test>, <Category: Third test>]
If you call save() with invalid data, you'll get a ValueError.
>>> f = CategoryForm({'name': '', 'slug': '', 'url': 'foo'})
>>> f = CategoryForm({'name': '', 'slug': 'not a slug!', 'url': 'foo'})
>>> f.errors['name']
[u'This field is required.']
>>> f.errors['slug']
[u'This field is required.']
[u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."]
>>> f.cleaned_data
Traceback (most recent call last):
...
Expand Down

0 comments on commit 5061970

Please sign in to comment.