Changes between Version 1 and Version 2 of UniqueSlugValidator


Ignore:
Timestamp:
06/22/2006 08:24:32 AM (9 years ago)
Author:
anonymous
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • UniqueSlugValidator

    v1 v2  
    1 
    2 {{{
    3 
    4 class MyModel(models.Model):
    5 
    6     name = models.CharField(maxlength=255)
    7    
    8     slug = models.SlugField()
    9    
    10 from django.core.validators import ValidationError
    11 from django.utils.translation import gettext, gettext_lazy
    12 
    13 def 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 
    27 for f in MyModel._meta.fields:
    28     if f.name == 'slug':
    29         f.validator_list.append(isUniqueSlug)
    30 
    31 }}}
Back to Top