Ticket #6281: slugfield-as-regexfield.patch

File slugfield-as-regexfield.patch, 1.8 KB (added by Joshua "jag" Ginsberg <jag@…>, 17 years ago)
  • trunk/django/db/models/fields/__init__.py

     
    10061006            kwargs['db_index'] = True
    10071007        super(SlugField, self).__init__(*args, **kwargs)
    10081008
     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
    10091024    def get_internal_type(self):
    10101025        return "SlugField"
    10111026
  • trunk/tests/modeltests/model_forms/models.py

     
    253253>>> Category.objects.order_by('name')
    254254[<Category: Entertainment>, <Category: It's a test>]
    255255
     256Invalid 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()
     259False
     260>>> 'slug' in f.errors
     261True
     262
    256263If you call save() with commit=False, then it will return an object that
    257264hasn't yet been saved to the database. In this case, it's up to you to call
    258265save() on the resulting model instance.
Back to Top