From 41b3a45fc569e0e7f5b8ee5b254ad8c1bb05ffb5 Mon Sep 17 00:00:00 2001 From: Gary Wilson Jr Date: Sat, 8 Sep 2007 05:09:39 +0000 Subject: [PATCH] Fixed #3557 -- Made `SlugField` inherit from `CharField` so that its `max_length` is properly set. Removed `get_manipulator_field_objs` method since it's already provided by `CharField`. Thanks, Russell Cloran. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6065 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/db/models/fields/__init__.py | 7 ++-- tests/modeltests/model_forms/models.py | 50 ++++++++++++++++---------- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/AUTHORS b/AUTHORS index 554f8325a7..7157b56dbc 100644 --- a/AUTHORS +++ b/AUTHORS @@ -79,6 +79,7 @@ answer newbie questions, and generally made Django that much better: Bryan Chow Michal Chruszcz Ian Clelland + Russell Cloran colin@owlfish.com crankycoder@gmail.com Pete Crosier diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index a727112b77..16c5d5212d 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -906,17 +906,14 @@ class PositiveSmallIntegerField(IntegerField): def get_manipulator_field_objs(self): return [oldforms.PositiveSmallIntegerField] -class SlugField(Field): +class SlugField(CharField): def __init__(self, *args, **kwargs): kwargs['max_length'] = kwargs.get('max_length', 50) kwargs.setdefault('validator_list', []).append(validators.isSlug) # Set db_index=True unless it's been set manually. if 'db_index' not in kwargs: kwargs['db_index'] = True - Field.__init__(self, *args, **kwargs) - - def get_manipulator_field_objs(self): - return [oldforms.TextField] + super(SlugField, self).__init__(*args, **kwargs) class SmallIntegerField(IntegerField): def get_manipulator_field_objs(self): diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index d27f0b0e54..47407573dd 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -32,6 +32,7 @@ ARTICLE_STATUS = ( class Category(models.Model): name = models.CharField(max_length=20) + slug = models.SlugField(max_length=20) url = models.CharField('The URL', max_length=40) def __unicode__(self): @@ -45,6 +46,7 @@ class Writer(models.Model): class Article(models.Model): headline = models.CharField(max_length=50) + slug = models.SlugField() pub_date = models.DateField() created = models.DateField(editable=False) writer = models.ForeignKey(Writer) @@ -79,9 +81,11 @@ __test__ = {'API_TESTS': """ >>> f = CategoryForm() >>> print f + >>> print f.as_ul()
  • +
  • >>> print f['name'] @@ -89,24 +93,25 @@ __test__ = {'API_TESTS': """ >>> f = CategoryForm(auto_id=False) >>> print f.as_ul()
  • Name:
  • +
  • Slug:
  • The URL:
  • ->>> f = CategoryForm({'name': 'Entertainment', 'url': 'entertainment'}) +>>> f = CategoryForm({'name': 'Entertainment', 'slug': 'entertainment', 'url': 'entertainment'}) >>> f.is_valid() True >>> f.cleaned_data -{'url': u'entertainment', 'name': u'Entertainment'} +{'url': u'entertainment', 'name': u'Entertainment', 'slug': u'entertainment'} >>> obj = f.save() >>> obj >>> Category.objects.all() [] ->>> f = CategoryForm({'name': "It's a test", 'url': 'test'}) +>>> f = CategoryForm({'name': "It's a test", 'slug': 'its-test', 'url': 'test'}) >>> f.is_valid() True >>> f.cleaned_data -{'url': u'test', 'name': u"It's a test"} +{'url': u'test', 'name': u"It's a test", 'slug': u'its-test'} >>> obj = f.save() >>> obj @@ -116,11 +121,11 @@ True If you call save() with commit=False, then it will return an object that hasn't yet been saved to the database. In this case, it's up to you to call save() on the resulting model instance. ->>> f = CategoryForm({'name': 'Third test', 'url': 'third'}) +>>> f = CategoryForm({'name': 'Third test', 'slug': 'third-test', 'url': 'third'}) >>> f.is_valid() True >>> f.cleaned_data -{'url': u'third', 'name': u'Third test'} +{'url': u'third', 'name': u'Third test', 'slug': u'third-test'} >>> obj = f.save(commit=False) >>> obj @@ -131,9 +136,9 @@ True [, , ] If you call save() with invalid data, you'll get a ValueError. ->>> f = CategoryForm({'name': '', 'url': 'foo'}) +>>> f = CategoryForm({'name': '', 'slug': '', 'url': 'foo'}) >>> f.errors -{'name': [u'This field is required.']} +{'name': [u'This field is required.'], 'slug': [u'This field is required.']} >>> f.cleaned_data Traceback (most recent call last): ... @@ -142,7 +147,7 @@ AttributeError: 'CategoryForm' object has no attribute 'cleaned_data' Traceback (most recent call last): ... ValueError: The Category could not be created because the data didn't validate. ->>> f = CategoryForm({'name': '', 'url': 'foo'}) +>>> f = CategoryForm({'name': '', 'slug': '', 'url': 'foo'}) >>> f.save() Traceback (most recent call last): ... @@ -160,6 +165,7 @@ fields with the 'choices' attribute are represented by a ChoiceField. >>> f = ArticleForm(auto_id=False) >>> print f Headline: +Slug: Pub date: Writer:
    Use both first and last names. ->>> art = Article(headline='Test article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.') +>>> art = Article(headline='Test article', slug='test-article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.') >>> art.save() >>> art.id 1 @@ -218,6 +224,7 @@ current values are inserted as 'initial' data in each Field. >>> f = TestArticleForm(auto_id=False) >>> print f.as_ul()
  • Headline:
  • +
  • Slug:
  • Pub date:
  • Writer: Hold down "Control", or "Command" on a Mac, to select more than one.
  • ->>> f = TestArticleForm({'headline': u'Test headline', 'pub_date': u'1984-02-06', 'writer': u'1', 'article': 'Hello.'}) +>>> f = TestArticleForm({'headline': u'Test headline', 'slug': 'test-headline', 'pub_date': u'1984-02-06', 'writer': u'1', 'article': 'Hello.'}) >>> f.is_valid() True >>> test_art = f.save() @@ -248,10 +255,11 @@ u'Test headline' You can create a form over a subset of the available fields by specifying a 'fields' argument to form_for_instance. ->>> PartialArticleForm = form_for_instance(art, fields=('headline','pub_date')) ->>> f = PartialArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04'}, auto_id=False) +>>> PartialArticleForm = form_for_instance(art, fields=('headline', 'slug', 'pub_date')) +>>> f = PartialArticleForm({'headline': u'New headline', 'slug': 'new-headline', 'pub_date': u'1988-01-04'}, auto_id=False) >>> print f.as_ul()
  • Headline:
  • +
  • Slug:
  • Pub date:
  • >>> f.is_valid() True @@ -272,6 +280,7 @@ Add some categories and test the many-to-many form output. >>> f = TestArticleForm(auto_id=False) >>> print f.as_ul()
  • Headline:
  • +
  • Slug:
  • Pub date:
  • Writer: Hold down "Control", or "Command" on a Mac, to select more than one.
  • ->>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04', +>>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04', ... 'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']}) >>> new_art = f.save() >>> new_art.id @@ -301,7 +310,7 @@ Add some categories and test the many-to-many form output. [, ] Now, submit form data with no categories. This deletes the existing categories. ->>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04', +>>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04', ... 'writer': u'1', 'article': u'Hello.'}) >>> new_art = f.save() >>> new_art.id @@ -312,7 +321,7 @@ Now, submit form data with no categories. This deletes the existing categories. Create a new article, with categories, via the form. >>> ArticleForm = form_for_model(Article) ->>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01', +>>> f = ArticleForm({'headline': u'The walrus was Paul', 'slug': u'walrus-was-paul', 'pub_date': u'1967-11-01', ... 'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']}) >>> new_art = f.save() >>> new_art.id @@ -323,7 +332,7 @@ Create a new article, with categories, via the form. Create a new article, with no categories, via the form. >>> ArticleForm = form_for_model(Article) ->>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01', +>>> f = ArticleForm({'headline': u'The walrus was Paul', 'slug': u'walrus-was-paul', 'pub_date': u'1967-11-01', ... 'writer': u'1', 'article': u'Test.'}) >>> new_art = f.save() >>> new_art.id @@ -335,7 +344,7 @@ Create a new article, with no categories, via the form. Create a new article, with categories, via the form, but use commit=False. The m2m data won't be saved until save_m2m() is invoked on the form. >>> ArticleForm = form_for_model(Article) ->>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01', +>>> f = ArticleForm({'headline': u'The walrus was Paul', 'slug': 'walrus-was-paul', 'pub_date': u'1967-11-01', ... 'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']}) >>> new_art = f.save(commit=False) @@ -359,13 +368,14 @@ the Category model, we can use save_instance() to apply its changes to an existing Category instance. >>> class ShortCategory(Form): ... name = CharField(max_length=5) +... slug = CharField(max_length=5) ... url = CharField(max_length=3) >>> cat = Category.objects.get(name='Third test') >>> cat >>> cat.id 3 ->>> sc = ShortCategory({'name': 'Third', 'url': '3rd'}) +>>> sc = ShortCategory({'name': 'Third', 'slug': 'third', 'url': '3rd'}) >>> save_instance(sc, cat) >>> Category.objects.get(id=3) @@ -378,6 +388,7 @@ the data in the database when the form is instantiated. >>> f = ArticleForm(auto_id=False) >>> print f.as_ul()
  • Headline:
  • +
  • Slug:
  • Pub date:
  • Writer:
  • +
  • Slug:
  • Pub date:
  • Writer: