Changes between Initial Version and Version 1 of UniqueSlugValidator


Ignore:
Timestamp:
Jun 22, 2006, 7:58:43 AM (18 years ago)
Author:
[530]
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • UniqueSlugValidator

    v1 v1  
     1
     2{{{
     3
     4class MyModel(models.Model):
     5
     6    name = models.CharField(maxlength=255)
     7   
     8    slug = models.SlugField()
     9   
     10from django.core.validators import ValidationError
     11from django.utils.translation import gettext, gettext_lazy
     12
     13def isUniqueSlug(field_data, all_data):
     14    if all_data.get('id', None):
     15        q=MyModel.objects.exclude(id=all_data.get('id'))
     16    else:
     17        q=MyModel.objects.all()
     18
     19    try:
     20        p=q.get(slug__exact=field_data)
     21    except:
     22        p=None
     23
     24    if p:
     25        raise ValidationError, gettext_lazy("Is not a unique slug.")
     26
     27for f in MyModel._meta.fields:
     28    if f.name == 'slug':
     29        f.validator_list.append(isUniqueSlug)
     30
     31}}}
Back to Top