Version 1 (modified by [530], 18 years ago) ( diff )

--

class MyModel(models.Model):

    name = models.CharField(maxlength=255)
    
    slug = models.SlugField()
    
from django.core.validators import ValidationError
from django.utils.translation import gettext, gettext_lazy

def isUniqueSlug(field_data, all_data):
    if all_data.get('id', None):
        q=MyModel.objects.exclude(id=all_data.get('id'))
    else:
        q=MyModel.objects.all()

    try:
        p=q.get(slug__exact=field_data)
    except:
        p=None

    if p:
        raise ValidationError, gettext_lazy("Is not a unique slug.")

for f in MyModel._meta.fields:
    if f.name == 'slug':
        f.validator_list.append(isUniqueSlug)

Note: See TracWiki for help on using the wiki.
Back to Top