Django

Code

Changeset 6065

Show
Ignore:
Timestamp:
09/08/07 00:09:39 (1 year ago)
Author:
gwilson
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r6028 r6065  
    8080    Michal Chruszcz <troll@pld-linux.org> 
    8181    Ian Clelland <clelland@gmail.com> 
     82    Russell Cloran <russell@rucus.net> 
    8283    colin@owlfish.com 
    8384    crankycoder@gmail.com 
  • django/trunk/django/db/models/fields/__init__.py

    r5826 r6065  
    907907        return [oldforms.PositiveSmallIntegerField] 
    908908 
    909 class SlugField(Field): 
     909class SlugField(CharField): 
    910910    def __init__(self, *args, **kwargs): 
    911911        kwargs['max_length'] = kwargs.get('max_length', 50) 
     
    914914        if 'db_index' not in kwargs: 
    915915            kwargs['db_index'] = True 
    916         Field.__init__(self, *args, **kwargs) 
    917  
    918     def get_manipulator_field_objs(self): 
    919         return [oldforms.TextField] 
     916        super(SlugField, self).__init__(*args, **kwargs) 
    920917 
    921918class SmallIntegerField(IntegerField): 
  • django/trunk/tests/modeltests/model_forms/models.py

    r5876 r6065  
    3333class Category(models.Model): 
    3434    name = models.CharField(max_length=20) 
     35    slug = models.SlugField(max_length=20) 
    3536    url = models.CharField('The URL', max_length=40) 
    3637 
     
    4647class Article(models.Model): 
    4748    headline = models.CharField(max_length=50) 
     49    slug = models.SlugField() 
    4850    pub_date = models.DateField() 
    4951    created = models.DateField(editable=False) 
     
    8082>>> print f 
    8183<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr> 
     84<tr><th><label for="id_slug">Slug:</label></th><td><input id="id_slug" type="text" name="slug" maxlength="20" /></td></tr> 
    8285<tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr> 
    8386>>> print f.as_ul() 
    8487<li><label for="id_name">Name:</label> <input id="id_name" type="text" name="name" maxlength="20" /></li> 
     88<li><label for="id_slug">Slug:</label> <input id="id_slug" type="text" name="slug" maxlength="20" /></li> 
    8589<li><label for="id_url">The URL:</label> <input id="id_url" type="text" name="url" maxlength="40" /></li> 
    8690>>> print f['name'] 
     
    9094>>> print f.as_ul() 
    9195<li>Name: <input type="text" name="name" maxlength="20" /></li> 
     96<li>Slug: <input type="text" name="slug" maxlength="20" /></li> 
    9297<li>The URL: <input type="text" name="url" maxlength="40" /></li> 
    9398 
    94 >>> f = CategoryForm({'name': 'Entertainment', 'url': 'entertainment'}) 
     99>>> f = CategoryForm({'name': 'Entertainment', 'slug': 'entertainment', 'url': 'entertainment'}) 
    95100>>> f.is_valid() 
    96101True 
    97102>>> f.cleaned_data 
    98 {'url': u'entertainment', 'name': u'Entertainment'
     103{'url': u'entertainment', 'name': u'Entertainment', 'slug': u'entertainment'
    99104>>> obj = f.save() 
    100105>>> obj 
     
    103108[<Category: Entertainment>] 
    104109 
    105 >>> f = CategoryForm({'name': "It's a test", 'url': 'test'}) 
     110>>> f = CategoryForm({'name': "It's a test", 'slug': 'its-test', 'url': 'test'}) 
    106111>>> f.is_valid() 
    107112True 
    108113>>> f.cleaned_data 
    109 {'url': u'test', 'name': u"It's a test"
     114{'url': u'test', 'name': u"It's a test", 'slug': u'its-test'
    110115>>> obj = f.save() 
    111116>>> obj 
     
    117122hasn't yet been saved to the database. In this case, it's up to you to call 
    118123save() on the resulting model instance. 
    119 >>> f = CategoryForm({'name': 'Third test', 'url': 'third'}) 
     124>>> f = CategoryForm({'name': 'Third test', 'slug': 'third-test', 'url': 'third'}) 
    120125>>> f.is_valid() 
    121126True 
    122127>>> f.cleaned_data 
    123 {'url': u'third', 'name': u'Third test'
     128{'url': u'third', 'name': u'Third test', 'slug': u'third-test'
    124129>>> obj = f.save(commit=False) 
    125130>>> obj 
     
    132137 
    133138If you call save() with invalid data, you'll get a ValueError. 
    134 >>> f = CategoryForm({'name': '', 'url': 'foo'}) 
     139>>> f = CategoryForm({'name': '', 'slug': '', 'url': 'foo'}) 
    135140>>> f.errors 
    136 {'name': [u'This field is required.']
     141{'name': [u'This field is required.'], 'slug': [u'This field is required.']
    137142>>> f.cleaned_data 
    138143Traceback (most recent call last): 
     
    143148... 
    144149ValueError: The Category could not be created because the data didn't validate. 
    145 >>> f = CategoryForm({'name': '', 'url': 'foo'}) 
     150>>> f = CategoryForm({'name': '', 'slug': '', 'url': 'foo'}) 
    146151>>> f.save() 
    147152Traceback (most recent call last): 
     
    161166>>> print f 
    162167<tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" /></td></tr> 
     168<tr><th>Slug:</th><td><input type="text" name="slug" maxlength="50" /></td></tr> 
    163169<tr><th>Pub date:</th><td><input type="text" name="pub_date" /></td></tr> 
    164170<tr><th>Writer:</th><td><select name="writer"> 
     
    211217<tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br />Use both first and last names.</td></tr> 
    212218 
    213 >>> art = Article(headline='Test article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.') 
     219>>> art = Article(headline='Test article', slug='test-article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.') 
    214220>>> art.save() 
    215221>>> art.id 
     
    219225>>> print f.as_ul() 
    220226<li>Headline: <input type="text" name="headline" value="Test article" maxlength="50" /></li> 
     227<li>Slug: <input type="text" name="slug" value="test-article" maxlength="50" /></li> 
    221228<li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li> 
    222229<li>Writer: <select name="writer"> 
     
    237244<option value="3">Third test</option> 
    238245</select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li> 
    239 >>> f = TestArticleForm({'headline': u'Test headline', 'pub_date': u'1984-02-06', 'writer': u'1', 'article': 'Hello.'}) 
     246>>> f = TestArticleForm({'headline': u'Test headline', 'slug': 'test-headline', 'pub_date': u'1984-02-06', 'writer': u'1', 'article': 'Hello.'}) 
    240247>>> f.is_valid() 
    241248True 
     
    249256You can create a form over a subset of the available fields 
    250257by specifying a 'fields' argument to form_for_instance. 
    251 >>> PartialArticleForm = form_for_instance(art, fields=('headline','pub_date')) 
    252 >>> f = PartialArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04'}, auto_id=False) 
     258>>> PartialArticleForm = form_for_instance(art, fields=('headline', 'slug', 'pub_date')) 
     259>>> f = PartialArticleForm({'headline': u'New headline', 'slug': 'new-headline', 'pub_date': u'1988-01-04'}, auto_id=False) 
    253260>>> print f.as_ul() 
    254261<li>Headline: <input type="text" name="headline" value="New headline" maxlength="50" /></li> 
     262<li>Slug: <input type="text" name="slug" value="new-headline" maxlength="50" /></li> 
    255263<li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li> 
    256264>>> f.is_valid() 
     
    273281>>> print f.as_ul() 
    274282<li>Headline: <input type="text" name="headline" value="New headline" maxlength="50" /></li> 
     283<li>Slug: <input type="text" name="slug" value="new-headline" maxlength="50" /></li> 
    275284<li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li> 
    276285<li>Writer: <select name="writer"> 
     
    292301</select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li> 
    293302 
    294 >>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04', 
     303>>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04', 
    295304...     'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']}) 
    296305>>> new_art = f.save() 
     
    302311 
    303312Now, submit form data with no categories. This deletes the existing categories. 
    304 >>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04', 
     313>>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04', 
    305314...     'writer': u'1', 'article': u'Hello.'}) 
    306315>>> new_art = f.save() 
     
    313322Create a new article, with categories, via the form. 
    314323>>> ArticleForm = form_for_model(Article) 
    315 >>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01', 
     324>>> f = ArticleForm({'headline': u'The walrus was Paul', 'slug': u'walrus-was-paul', 'pub_date': u'1967-11-01', 
    316325...     'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']}) 
    317326>>> new_art = f.save() 
     
    324333Create a new article, with no categories, via the form. 
    325334>>> ArticleForm = form_for_model(Article) 
    326 >>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01', 
     335>>> f = ArticleForm({'headline': u'The walrus was Paul', 'slug': u'walrus-was-paul', 'pub_date': u'1967-11-01', 
    327336...     'writer': u'1', 'article': u'Test.'}) 
    328337>>> new_art = f.save() 
     
    336345The m2m data won't be saved until save_m2m() is invoked on the form. 
    337346>>> ArticleForm = form_for_model(Article) 
    338 >>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01', 
     347>>> f = ArticleForm({'headline': u'The walrus was Paul', 'slug': 'walrus-was-paul', 'pub_date': u'1967-11-01', 
    339348...     'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']}) 
    340349>>> new_art = f.save(commit=False) 
     
    360369>>> class ShortCategory(Form): 
    361370...     name = CharField(max_length=5) 
     371...     slug = CharField(max_length=5) 
    362372...     url = CharField(max_length=3) 
    363373>>> cat = Category.objects.get(name='Third test') 
     
    366376>>> cat.id 
    3673773 
    368 >>> sc = ShortCategory({'name': 'Third', 'url': '3rd'}) 
     378>>> sc = ShortCategory({'name': 'Third', 'slug': 'third', 'url': '3rd'}) 
    369379>>> save_instance(sc, cat) 
    370380<Category: Third> 
     
    379389>>> print f.as_ul() 
    380390<li>Headline: <input type="text" name="headline" maxlength="50" /></li> 
     391<li>Slug: <input type="text" name="slug" maxlength="50" /></li> 
    381392<li>Pub date: <input type="text" name="pub_date" /></li> 
    382393<li>Writer: <select name="writer"> 
     
    403414>>> print f.as_ul() 
    404415<li>Headline: <input type="text" name="headline" maxlength="50" /></li> 
     416<li>Slug: <input type="text" name="slug" maxlength="50" /></li> 
    405417<li>Pub date: <input type="text" name="pub_date" /></li> 
    406418<li>Writer: <select name="writer">