Ticket #6281: slugfield-as-regexfield.patch
File slugfield-as-regexfield.patch, 1.8 KB (added by , 17 years ago) |
---|
-
trunk/django/db/models/fields/__init__.py
1006 1006 kwargs['db_index'] = True 1007 1007 super(SlugField, self).__init__(*args, **kwargs) 1008 1008 1009 def formfield(self, **kwargs): 1010 # max_length and error_message can be overridden 1011 kwargs['max_length'] = kwargs.get('max_length', self.max_length) 1012 kwargs['error_message'] = kwargs.get( 1013 'error_message', 1014 _(u'Please enter a value containing only letters, numbers, ' 1015 u'dashes, and underscores.') 1016 ) 1017 defaults = {'regex': r'^[-\w]+$', # from django.core.validators.slug_re 1018 'form_class': forms.RegexField, 1019 } 1020 # other defaults cannot be overridden 1021 kwargs.update(defaults) 1022 return super(SlugField, self).formfield(**kwargs) 1023 1009 1024 def get_internal_type(self): 1010 1025 return "SlugField" 1011 1026 -
trunk/tests/modeltests/model_forms/models.py
253 253 >>> Category.objects.order_by('name') 254 254 [<Category: Entertainment>, <Category: It's a test>] 255 255 256 Invalid slug format test. 257 >>> f = CategoryForm({'name': u"It's a bad slug test", 'slug': u'bad slug, no biscuit', 'url': u'test'}) 258 >>> f.is_valid() 259 False 260 >>> 'slug' in f.errors 261 True 262 256 263 If you call save() with commit=False, then it will return an object that 257 264 hasn't yet been saved to the database. In this case, it's up to you to call 258 265 save() on the resulting model instance.