Author: gwilson
Date: 2007-09-08 00:09:39 -0500 (Sat, 08 Sep 2007)
New Revision: 6065

Modified:
   django/trunk/AUTHORS
   django/trunk/django/db/models/fields/__init__.py
   django/trunk/tests/modeltests/model_forms/models.py
Log:
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.


Modified: django/trunk/AUTHORS
===================================================================
--- django/trunk/AUTHORS        2007-09-07 20:51:17 UTC (rev 6064)
+++ django/trunk/AUTHORS        2007-09-08 05:09:39 UTC (rev 6065)
@@ -79,6 +79,7 @@
     Bryan Chow <bryan at verdjn dot com>
     Michal Chruszcz <[EMAIL PROTECTED]>
     Ian Clelland <[EMAIL PROTECTED]>
+    Russell Cloran <[EMAIL PROTECTED]>
     [EMAIL PROTECTED]
     [EMAIL PROTECTED]
     Pete Crosier <[EMAIL PROTECTED]>

Modified: django/trunk/django/db/models/fields/__init__.py
===================================================================
--- django/trunk/django/db/models/fields/__init__.py    2007-09-07 20:51:17 UTC 
(rev 6064)
+++ django/trunk/django/db/models/fields/__init__.py    2007-09-08 05:09:39 UTC 
(rev 6065)
@@ -906,18 +906,15 @@
     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)
+        super(SlugField, self).__init__(*args, **kwargs)
 
-    def get_manipulator_field_objs(self):
-        return [oldforms.TextField]
-
 class SmallIntegerField(IntegerField):
     def get_manipulator_field_objs(self):
         return [oldforms.SmallIntegerField]

Modified: django/trunk/tests/modeltests/model_forms/models.py
===================================================================
--- django/trunk/tests/modeltests/model_forms/models.py 2007-09-07 20:51:17 UTC 
(rev 6064)
+++ django/trunk/tests/modeltests/model_forms/models.py 2007-09-08 05:09:39 UTC 
(rev 6065)
@@ -32,6 +32,7 @@
 
 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 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 @@
 >>> f = CategoryForm()
 >>> print f
 <tr><th><label for="id_name">Name:</label></th><td><input id="id_name" 
type="text" name="name" maxlength="20" /></td></tr>
+<tr><th><label for="id_slug">Slug:</label></th><td><input id="id_slug" 
type="text" name="slug" maxlength="20" /></td></tr>
 <tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" 
type="text" name="url" maxlength="40" /></td></tr>
 >>> print f.as_ul()
 <li><label for="id_name">Name:</label> <input id="id_name" type="text" 
name="name" maxlength="20" /></li>
+<li><label for="id_slug">Slug:</label> <input id="id_slug" type="text" 
name="slug" maxlength="20" /></li>
 <li><label for="id_url">The URL:</label> <input id="id_url" type="text" 
name="url" maxlength="40" /></li>
 >>> print f['name']
 <input id="id_name" type="text" name="name" maxlength="20" />
@@ -89,24 +93,25 @@
 >>> f = CategoryForm(auto_id=False)
 >>> print f.as_ul()
 <li>Name: <input type="text" name="name" maxlength="20" /></li>
+<li>Slug: <input type="text" name="slug" maxlength="20" /></li>
 <li>The URL: <input type="text" name="url" maxlength="40" /></li>
 
->>> 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: Entertainment>
 >>> Category.objects.all()
 [<Category: Entertainment>]
 
->>> 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
 <Category: It's a test>
@@ -116,11 +121,11 @@
 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
 <Category: Third test>
@@ -131,9 +136,9 @@
 [<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': '', '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 @@
 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 @@
 >>> f = ArticleForm(auto_id=False)
 >>> print f
 <tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" 
/></td></tr>
+<tr><th>Slug:</th><td><input type="text" name="slug" maxlength="50" 
/></td></tr>
 <tr><th>Pub date:</th><td><input type="text" name="pub_date" /></td></tr>
 <tr><th>Writer:</th><td><select name="writer">
 <option value="" selected="selected">---------</option>
@@ -210,7 +216,7 @@
 >>> print f
 <tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" 
maxlength="50" /><br />Use both first and last names.</td></tr>
 
->>> 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 @@
 >>> f = TestArticleForm(auto_id=False)
 >>> print f.as_ul()
 <li>Headline: <input type="text" name="headline" value="Test article" 
maxlength="50" /></li>
+<li>Slug: <input type="text" name="slug" value="test-article" maxlength="50" 
/></li>
 <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li>
 <li>Writer: <select name="writer">
 <option value="">---------</option>
@@ -236,7 +243,7 @@
 <option value="2">It&#39;s a test</option>
 <option value="3">Third test</option>
 </select>  Hold down "Control", or "Command" on a Mac, to select more than 
one.</li>
->>> 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 @@
 
 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()
 <li>Headline: <input type="text" name="headline" value="New headline" 
maxlength="50" /></li>
+<li>Slug: <input type="text" name="slug" value="new-headline" maxlength="50" 
/></li>
 <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li>
 >>> f.is_valid()
 True
@@ -272,6 +280,7 @@
 >>> f = TestArticleForm(auto_id=False)
 >>> print f.as_ul()
 <li>Headline: <input type="text" name="headline" value="New headline" 
maxlength="50" /></li>
+<li>Slug: <input type="text" name="slug" value="new-headline" maxlength="50" 
/></li>
 <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li>
 <li>Writer: <select name="writer">
 <option value="">---------</option>
@@ -291,7 +300,7 @@
 <option value="3">Third test</option>
 </select>  Hold down "Control", or "Command" on a Mac, to select more than 
one.</li>
 
->>> 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 @@
 [<Category: Entertainment>, <Category: It's a test>]
 
 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 @@
 
 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 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 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 @@
 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
 <Category: Third test>
 >>> cat.id
 3
->>> sc = ShortCategory({'name': 'Third', 'url': '3rd'})
+>>> sc = ShortCategory({'name': 'Third', 'slug': 'third', 'url': '3rd'})
 >>> save_instance(sc, cat)
 <Category: Third>
 >>> Category.objects.get(id=3)
@@ -378,6 +388,7 @@
 >>> f = ArticleForm(auto_id=False)
 >>> print f.as_ul()
 <li>Headline: <input type="text" name="headline" maxlength="50" /></li>
+<li>Slug: <input type="text" name="slug" maxlength="50" /></li>
 <li>Pub date: <input type="text" name="pub_date" /></li>
 <li>Writer: <select name="writer">
 <option value="" selected="selected">---------</option>
@@ -402,6 +413,7 @@
 <Writer: Carl Bernstein>
 >>> print f.as_ul()
 <li>Headline: <input type="text" name="headline" maxlength="50" /></li>
+<li>Slug: <input type="text" name="slug" maxlength="50" /></li>
 <li>Pub date: <input type="text" name="pub_date" /></li>
 <li>Writer: <select name="writer">
 <option value="" selected="selected">---------</option>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to