Changeset 6065
- Timestamp:
- 09/08/07 00:09:39 (1 year ago)
- Files:
-
- django/trunk/AUTHORS (modified) (1 diff)
- django/trunk/django/db/models/fields/__init__.py (modified) (2 diffs)
- django/trunk/tests/modeltests/model_forms/models.py (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/AUTHORS
r6028 r6065 80 80 Michal Chruszcz <troll@pld-linux.org> 81 81 Ian Clelland <clelland@gmail.com> 82 Russell Cloran <russell@rucus.net> 82 83 colin@owlfish.com 83 84 crankycoder@gmail.com django/trunk/django/db/models/fields/__init__.py
r5826 r6065 907 907 return [oldforms.PositiveSmallIntegerField] 908 908 909 class SlugField( Field):909 class SlugField(CharField): 910 910 def __init__(self, *args, **kwargs): 911 911 kwargs['max_length'] = kwargs.get('max_length', 50) … … 914 914 if 'db_index' not in kwargs: 915 915 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) 920 917 921 918 class SmallIntegerField(IntegerField): django/trunk/tests/modeltests/model_forms/models.py
r5876 r6065 33 33 class Category(models.Model): 34 34 name = models.CharField(max_length=20) 35 slug = models.SlugField(max_length=20) 35 36 url = models.CharField('The URL', max_length=40) 36 37 … … 46 47 class Article(models.Model): 47 48 headline = models.CharField(max_length=50) 49 slug = models.SlugField() 48 50 pub_date = models.DateField() 49 51 created = models.DateField(editable=False) … … 80 82 >>> print f 81 83 <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> 82 85 <tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr> 83 86 >>> print f.as_ul() 84 87 <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> 85 89 <li><label for="id_url">The URL:</label> <input id="id_url" type="text" name="url" maxlength="40" /></li> 86 90 >>> print f['name'] … … 90 94 >>> print f.as_ul() 91 95 <li>Name: <input type="text" name="name" maxlength="20" /></li> 96 <li>Slug: <input type="text" name="slug" maxlength="20" /></li> 92 97 <li>The URL: <input type="text" name="url" maxlength="40" /></li> 93 98 94 >>> f = CategoryForm({'name': 'Entertainment', ' url': 'entertainment'})99 >>> f = CategoryForm({'name': 'Entertainment', 'slug': 'entertainment', 'url': 'entertainment'}) 95 100 >>> f.is_valid() 96 101 True 97 102 >>> f.cleaned_data 98 {'url': u'entertainment', 'name': u'Entertainment' }103 {'url': u'entertainment', 'name': u'Entertainment', 'slug': u'entertainment'} 99 104 >>> obj = f.save() 100 105 >>> obj … … 103 108 [<Category: Entertainment>] 104 109 105 >>> f = CategoryForm({'name': "It's a test", ' url': 'test'})110 >>> f = CategoryForm({'name': "It's a test", 'slug': 'its-test', 'url': 'test'}) 106 111 >>> f.is_valid() 107 112 True 108 113 >>> 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'} 110 115 >>> obj = f.save() 111 116 >>> obj … … 117 122 hasn't yet been saved to the database. In this case, it's up to you to call 118 123 save() on the resulting model instance. 119 >>> f = CategoryForm({'name': 'Third test', ' url': 'third'})124 >>> f = CategoryForm({'name': 'Third test', 'slug': 'third-test', 'url': 'third'}) 120 125 >>> f.is_valid() 121 126 True 122 127 >>> f.cleaned_data 123 {'url': u'third', 'name': u'Third test' }128 {'url': u'third', 'name': u'Third test', 'slug': u'third-test'} 124 129 >>> obj = f.save(commit=False) 125 130 >>> obj … … 132 137 133 138 If you call save() with invalid data, you'll get a ValueError. 134 >>> f = CategoryForm({'name': '', ' url': 'foo'})139 >>> f = CategoryForm({'name': '', 'slug': '', 'url': 'foo'}) 135 140 >>> f.errors 136 {'name': [u'This field is required.'] }141 {'name': [u'This field is required.'], 'slug': [u'This field is required.']} 137 142 >>> f.cleaned_data 138 143 Traceback (most recent call last): … … 143 148 ... 144 149 ValueError: 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'}) 146 151 >>> f.save() 147 152 Traceback (most recent call last): … … 161 166 >>> print f 162 167 <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> 163 169 <tr><th>Pub date:</th><td><input type="text" name="pub_date" /></td></tr> 164 170 <tr><th>Writer:</th><td><select name="writer"> … … 211 217 <tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br />Use both first and last names.</td></tr> 212 218 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.') 214 220 >>> art.save() 215 221 >>> art.id … … 219 225 >>> print f.as_ul() 220 226 <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> 221 228 <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li> 222 229 <li>Writer: <select name="writer"> … … 237 244 <option value="3">Third test</option> 238 245 </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.'}) 240 247 >>> f.is_valid() 241 248 True … … 249 256 You can create a form over a subset of the available fields 250 257 by 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) 253 260 >>> print f.as_ul() 254 261 <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> 255 263 <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li> 256 264 >>> f.is_valid() … … 273 281 >>> print f.as_ul() 274 282 <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> 275 284 <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li> 276 285 <li>Writer: <select name="writer"> … … 292 301 </select> Hold down "Control", or "Command" on a Mac, to select more than one.</li> 293 302 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', 295 304 ... 'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']}) 296 305 >>> new_art = f.save() … … 302 311 303 312 Now, 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', 305 314 ... 'writer': u'1', 'article': u'Hello.'}) 306 315 >>> new_art = f.save() … … 313 322 Create a new article, with categories, via the form. 314 323 >>> 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', 316 325 ... 'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']}) 317 326 >>> new_art = f.save() … … 324 333 Create a new article, with no categories, via the form. 325 334 >>> 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', 327 336 ... 'writer': u'1', 'article': u'Test.'}) 328 337 >>> new_art = f.save() … … 336 345 The m2m data won't be saved until save_m2m() is invoked on the form. 337 346 >>> 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', 339 348 ... 'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']}) 340 349 >>> new_art = f.save(commit=False) … … 360 369 >>> class ShortCategory(Form): 361 370 ... name = CharField(max_length=5) 371 ... slug = CharField(max_length=5) 362 372 ... url = CharField(max_length=3) 363 373 >>> cat = Category.objects.get(name='Third test') … … 366 376 >>> cat.id 367 377 3 368 >>> sc = ShortCategory({'name': 'Third', ' url': '3rd'})378 >>> sc = ShortCategory({'name': 'Third', 'slug': 'third', 'url': '3rd'}) 369 379 >>> save_instance(sc, cat) 370 380 <Category: Third> … … 379 389 >>> print f.as_ul() 380 390 <li>Headline: <input type="text" name="headline" maxlength="50" /></li> 391 <li>Slug: <input type="text" name="slug" maxlength="50" /></li> 381 392 <li>Pub date: <input type="text" name="pub_date" /></li> 382 393 <li>Writer: <select name="writer"> … … 403 414 >>> print f.as_ul() 404 415 <li>Headline: <input type="text" name="headline" maxlength="50" /></li> 416 <li>Slug: <input type="text" name="slug" maxlength="50" /></li> 405 417 <li>Pub date: <input type="text" name="pub_date" /></li> 406 418 <li>Writer: <select name="writer">
