Ticket #3557: slugfield.2.patch
File slugfield.2.patch, 9.9 KB (added by , 18 years ago) |
---|
-
django/db/models/fields/__init__.py
762 762 def get_manipulator_field_objs(self): 763 763 return [oldforms.PositiveSmallIntegerField] 764 764 765 class SlugField( Field):765 class SlugField(CharField): 766 766 def __init__(self, *args, **kwargs): 767 767 kwargs['maxlength'] = kwargs.get('maxlength', 50) 768 768 kwargs.setdefault('validator_list', []).append(validators.isSlug) 769 769 # Set db_index=True unless it's been set manually. 770 770 if not kwargs.has_key('db_index'): 771 771 kwargs['db_index'] = True 772 Field.__init__(self, *args, **kwargs)772 CharField.__init__(self, *args, **kwargs) 773 773 774 def get_manipulator_field_objs(self):775 return [oldforms.TextField]776 777 774 class SmallIntegerField(IntegerField): 778 775 def get_manipulator_field_objs(self): 779 776 return [oldforms.SmallIntegerField] -
tests/modeltests/model_forms/models.py
25 25 from django.db import models 26 26 27 27 class Category(models.Model): 28 slug = models.SlugField(maxlength=20) 28 29 name = models.CharField(maxlength=20) 29 30 url = models.CharField('The URL', maxlength=40) 30 31 … … 38 39 return self.name 39 40 40 41 class Article(models.Model): 42 slug = models.SlugField() 41 43 headline = models.CharField(maxlength=50) 42 44 pub_date = models.DateField() 43 45 created = models.DateField(editable=False) … … 71 73 >>> CategoryForm = form_for_model(Category) 72 74 >>> f = CategoryForm() 73 75 >>> print f 76 <tr><th><label for="id_slug">Slug:</label></th><td><input id="id_slug" type="text" name="slug" maxlength="20" /></td></tr> 74 77 <tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr> 75 78 <tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr> 76 79 >>> print f.as_ul() 80 <li><label for="id_slug">Slug:</label> <input id="id_slug" type="text" name="slug" maxlength="20" /></li> 77 81 <li><label for="id_name">Name:</label> <input id="id_name" type="text" name="name" maxlength="20" /></li> 78 82 <li><label for="id_url">The URL:</label> <input id="id_url" type="text" name="url" maxlength="40" /></li> 79 83 >>> print f['name'] … … 81 85 82 86 >>> f = CategoryForm(auto_id=False) 83 87 >>> print f.as_ul() 88 <li>Slug: <input type="text" name="slug" maxlength="20" /></li> 84 89 <li>Name: <input type="text" name="name" maxlength="20" /></li> 85 90 <li>The URL: <input type="text" name="url" maxlength="40" /></li> 86 91 87 >>> f = CategoryForm({' name': 'Entertainment', 'url': 'entertainment'})92 >>> f = CategoryForm({'slug': 'entertainment', 'name': 'Entertainment', 'url': 'entertainment'}) 88 93 >>> f.is_valid() 89 94 True 90 95 >>> f.clean_data 91 {'url': u'entertainment', ' name': u'Entertainment'}96 {'url': u'entertainment', 'slug': u'entertainment', 'name': u'Entertainment'} 92 97 >>> obj = f.save() 93 98 >>> obj 94 99 <Category: Entertainment> 95 100 >>> Category.objects.all() 96 101 [<Category: Entertainment>] 97 102 98 >>> f = CategoryForm({' name': "It's a test", 'url': 'test'})103 >>> f = CategoryForm({'slug': 'its-test', 'name': "It's a test", 'url': 'test'}) 99 104 >>> f.is_valid() 100 105 True 101 106 >>> f.clean_data 102 {'url': u'test', ' name': u"It's a test"}107 {'url': u'test', 'slug': u'its-test', 'name': u"It's a test"} 103 108 >>> obj = f.save() 104 109 >>> obj 105 110 <Category: It's a test> … … 109 114 If you call save() with commit=False, then it will return an object that 110 115 hasn't yet been saved to the database. In this case, it's up to you to call 111 116 save() on the resulting model instance. 112 >>> f = CategoryForm({' name': 'Third test', 'url': 'third'})117 >>> f = CategoryForm({'slug': 'third-test', 'name': 'Third test', 'url': 'third'}) 113 118 >>> f.is_valid() 114 119 True 115 120 >>> f.clean_data 116 {'url': u'third', ' name': u'Third test'}121 {'url': u'third', 'slug': u'third-test', 'name': u'Third test'} 117 122 >>> obj = f.save(commit=False) 118 123 >>> obj 119 124 <Category: Third test> … … 124 129 [<Category: Entertainment>, <Category: It's a test>, <Category: Third test>] 125 130 126 131 If you call save() with invalid data, you'll get a ValueError. 127 >>> f = CategoryForm({' name': '', 'url': 'foo'})132 >>> f = CategoryForm({'slug': '', 'name': '', 'url': 'foo'}) 128 133 >>> f.errors 129 {' name': [u'This field is required.']}134 {'slug': [u'This field is required.'], 'name': [u'This field is required.']} 130 135 >>> f.clean_data 131 136 Traceback (most recent call last): 132 137 ... … … 135 140 Traceback (most recent call last): 136 141 ... 137 142 ValueError: The Category could not be created because the data didn't validate. 138 >>> f = CategoryForm({' name': '', 'url': 'foo'})143 >>> f = CategoryForm({'slug': '', 'name': '', 'url': 'foo'}) 139 144 >>> f.save() 140 145 Traceback (most recent call last): 141 146 ... … … 152 157 >>> ArticleForm = form_for_model(Article) 153 158 >>> f = ArticleForm(auto_id=False) 154 159 >>> print f 160 <tr><th>Slug:</th><td><input type="text" name="slug" maxlength="50" /></td></tr> 155 161 <tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" /></td></tr> 156 162 <tr><th>Pub date:</th><td><input type="text" name="pub_date" /></td></tr> 157 163 <tr><th>Writer:</th><td><select name="writer"> … … 185 191 >>> print f 186 192 <tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br />Use both first and last names.</td></tr> 187 193 188 >>> art = Article( headline='Test article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.')194 >>> art = Article(slug='test-article', headline='Test article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.') 189 195 >>> art.save() 190 196 >>> art.id 191 197 1 192 198 >>> TestArticleForm = form_for_instance(art) 193 199 >>> f = TestArticleForm(auto_id=False) 194 200 >>> print f.as_ul() 201 <li>Slug: <input type="text" name="slug" value="test-article" maxlength="50" /></li> 195 202 <li>Headline: <input type="text" name="headline" value="Test article" maxlength="50" /></li> 196 203 <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li> 197 204 <li>Writer: <select name="writer"> … … 205 212 <option value="2">It's a test</option> 206 213 <option value="3">Third test</option> 207 214 </select> Hold down "Control", or "Command" on a Mac, to select more than one.</li> 208 >>> f = TestArticleForm({' headline': u'New headline', 'pub_date': u'1988-01-04', 'writer': u'1', 'article': 'Hello.'})215 >>> f = TestArticleForm({'slug': 'new-headline', 'headline': u'New headline', 'pub_date': u'1988-01-04', 'writer': u'1', 'article': 'Hello.'}) 209 216 >>> f.is_valid() 210 217 True 211 218 >>> new_art = f.save() … … 224 231 >>> TestArticleForm = form_for_instance(new_art) 225 232 >>> f = TestArticleForm(auto_id=False) 226 233 >>> print f.as_ul() 234 <li>Slug: <input type="text" name="slug" value="new-headline" maxlength="50" /></li> 227 235 <li>Headline: <input type="text" name="headline" value="New headline" maxlength="50" /></li> 228 236 <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li> 229 237 <li>Writer: <select name="writer"> … … 238 246 <option value="3">Third test</option> 239 247 </select> Hold down "Control", or "Command" on a Mac, to select more than one.</li> 240 248 241 >>> f = TestArticleForm({' headline': u'New headline', 'pub_date': u'1988-01-04',249 >>> f = TestArticleForm({'slug': u'new-headline', 'headline': u'New headline', 'pub_date': u'1988-01-04', 242 250 ... 'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']}) 243 251 >>> new_art = f.save() 244 252 >>> new_art.id … … 248 256 [<Category: Entertainment>, <Category: It's a test>] 249 257 250 258 Now, submit form data with no categories. This deletes the existing categories. 251 >>> f = TestArticleForm({' headline': u'New headline', 'pub_date': u'1988-01-04',259 >>> f = TestArticleForm({'slug': u'new-headline', 'headline': u'New headline', 'pub_date': u'1988-01-04', 252 260 ... 'writer': u'1', 'article': u'Hello.'}) 253 261 >>> new_art = f.save() 254 262 >>> new_art.id … … 259 267 260 268 Create a new article, with categories, via the form. 261 269 >>> ArticleForm = form_for_model(Article) 262 >>> f = ArticleForm({' headline': u'The walrus was Paul', 'pub_date': u'1967-11-01',270 >>> f = ArticleForm({'slug': u'new-headline', 'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01', 263 271 ... 'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']}) 264 272 >>> new_art = f.save() 265 273 >>> new_art.id … … 270 278 271 279 Create a new article, with no categories, via the form. 272 280 >>> ArticleForm = form_for_model(Article) 273 >>> f = ArticleForm({' headline': u'The walrus was Paul', 'pub_date': u'1967-11-01',281 >>> f = ArticleForm({'slug': u'walrus-was-paul', 'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01', 274 282 ... 'writer': u'1', 'article': u'Test.'}) 275 283 >>> new_art = f.save() 276 284 >>> new_art.id … … 283 291 the Category model, we can use save_instance() to apply its changes to an 284 292 existing Category instance. 285 293 >>> class ShortCategory(Form): 294 ... slug = CharField(max_length=5) 286 295 ... name = CharField(max_length=5) 287 296 ... url = CharField(max_length=3) 288 297 >>> cat = Category.objects.get(name='Third test') … … 290 299 <Category: Third test> 291 300 >>> cat.id 292 301 3 293 >>> sc = ShortCategory({' name': 'Third', 'url': '3rd'})302 >>> sc = ShortCategory({'slug': 'third', 'name': 'Third', 'url': '3rd'}) 294 303 >>> save_instance(sc, cat) 295 304 <Category: Third> 296 305 >>> Category.objects.get(id=3) … … 302 311 >>> ArticleForm = form_for_model(Article) 303 312 >>> f = ArticleForm(auto_id=False) 304 313 >>> print f.as_ul() 314 <li>Slug: <input type="text" name="slug" maxlength="50" /></li> 305 315 <li>Headline: <input type="text" name="headline" maxlength="50" /></li> 306 316 <li>Pub date: <input type="text" name="pub_date" /></li> 307 317 <li>Writer: <select name="writer"> … … 320 330 >>> Writer.objects.create(name='Carl Bernstein') 321 331 <Writer: Carl Bernstein> 322 332 >>> print f.as_ul() 333 <li>Slug: <input type="text" name="slug" maxlength="50" /></li> 323 334 <li>Headline: <input type="text" name="headline" maxlength="50" /></li> 324 335 <li>Pub date: <input type="text" name="pub_date" /></li> 325 336 <li>Writer: <select name="writer">