from django.utils.text import slugify def generate_unique_slug(klass, field, instance=None): """ return unique slug if origin slug is exist. eg: `foo-bar` => `foo-bar-1` :param `klass` is Class model. :param `field` is specific field for title. :param `instance` is instance object for excluding specific object. """ origin_slug = slugify(field) unique_slug = origin_slug numb = 1 if instance is not None: while klass.objects.filter(slug=unique_slug).exclude(id=instance.id).exists(): unique_slug = '%s-%d' % (origin_slug, numb) numb += 1 else: while klass.objects.filter(slug=unique_slug).exists(): unique_slug = '%s-%d' % (origin_slug, numb) numb += 1 return unique_slug """ # USAGE EXAMPLE from django.db import models from django.contrib.auth.models import User from myapp.utils import generate_unique_slug class Post(models.Model): author = models.ForeignKey(User, on_delete=models.CASCADE) title = models.CharField(max_length=200) slug = models.SlugField(max_length=200, unique=True) def save(self, *args, **kwargs): if self.slug: # edit if slugify(self.title) != self.slug: self.slug = generate_unique_slug(Post, self.title) else: # create self.slug = generate_unique_slug(Post, self.title) super(Post, self).save(*args, **kwargs) """