Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 41b3a45fc569e0e7f5b8ee5b254ad8c1bb05ffb5 1 parent be39ade
Gary Wilson Jr. authored September 08, 2007
1  AUTHORS
@@ -79,6 +79,7 @@ answer newbie questions, and generally made Django that much better:
79 79
     Bryan Chow <bryan at verdjn dot com>
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
84 85
     Pete Crosier <pete.crosier@gmail.com>
7  django/db/models/fields/__init__.py
@@ -906,17 +906,14 @@ class PositiveSmallIntegerField(IntegerField):
906 906
     def get_manipulator_field_objs(self):
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)
912 912
         kwargs.setdefault('validator_list', []).append(validators.isSlug)
913 913
         # Set db_index=True unless it's been set manually.
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):
922 919
     def get_manipulator_field_objs(self):
50  tests/modeltests/model_forms/models.py
@@ -32,6 +32,7 @@
32 32
 
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
 
37 38
     def __unicode__(self):
@@ -45,6 +46,7 @@ def __unicode__(self):
45 46
 
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)
50 52
     writer = models.ForeignKey(Writer)
@@ -79,9 +81,11 @@ def __unicode__(self):
79 81
 >>> f = CategoryForm()
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']
87 91
 <input id="id_name" type="text" name="name" maxlength="20" />
@@ -89,24 +93,25 @@ def __unicode__(self):
89 93
 >>> f = CategoryForm(auto_id=False)
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
101 106
 <Category: Entertainment>
102 107
 >>> Category.objects.all()
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
112 117
 <Category: It's a test>
@@ -116,11 +121,11 @@ def __unicode__(self):
116 121
 If you call save() with commit=False, then it will return an object that
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
126 131
 <Category: Third test>
@@ -131,9 +136,9 @@ def __unicode__(self):
131 136
 [<Category: Entertainment>, <Category: It's a test>, <Category: Third test>]
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):
139 144
 ...
@@ -142,7 +147,7 @@ def __unicode__(self):
142 147
 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):
148 153
 ...
@@ -160,6 +165,7 @@ def __unicode__(self):
160 165
 >>> f = ArticleForm(auto_id=False)
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">
165 171
 <option value="" selected="selected">---------</option>
@@ -210,7 +216,7 @@ def __unicode__(self):
210 216
 >>> print f
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
216 222
 1
@@ -218,6 +224,7 @@ def __unicode__(self):
218 224
 >>> f = TestArticleForm(auto_id=False)
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">
223 230
 <option value="">---------</option>
@@ -236,7 +243,7 @@ def __unicode__(self):
236 243
 <option value="2">It&#39;s a test</option>
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
242 249
 >>> test_art = f.save()
@@ -248,10 +255,11 @@ def __unicode__(self):
248 255
 
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()
257 265
 True
@@ -272,6 +280,7 @@ def __unicode__(self):
272 280
 >>> f = TestArticleForm(auto_id=False)
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">
277 286
 <option value="">---------</option>
@@ -291,7 +300,7 @@ def __unicode__(self):
291 300
 <option value="3">Third test</option>
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()
297 306
 >>> new_art.id
@@ -301,7 +310,7 @@ def __unicode__(self):
301 310
 [<Category: Entertainment>, <Category: It's a test>]
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()
307 316
 >>> new_art.id
@@ -312,7 +321,7 @@ def __unicode__(self):
312 321
 
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()
318 327
 >>> new_art.id
@@ -323,7 +332,7 @@ def __unicode__(self):
323 332
 
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()
329 338
 >>> new_art.id
@@ -335,7 +344,7 @@ def __unicode__(self):
335 344
 Create a new article, with categories, via the form, but use commit=False.
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)
341 350
 
@@ -359,13 +368,14 @@ def __unicode__(self):
359 368
 existing Category instance.
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')
364 374
 >>> cat
365 375
 <Category: 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>
371 381
 >>> Category.objects.get(id=3)
@@ -378,6 +388,7 @@ def __unicode__(self):
378 388
 >>> f = ArticleForm(auto_id=False)
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">
383 394
 <option value="" selected="selected">---------</option>
@@ -402,6 +413,7 @@ def __unicode__(self):
402 413
 <Writer: Carl Bernstein>
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">
407 419
 <option value="" selected="selected">---------</option>

0 notes on commit 41b3a45

Please sign in to comment.
Something went wrong with that request. Please try again.