Generate slug in Django Model

A slug is a unique identifier or we can say a unique label associated with each record. This is useful when you write the rest APIs. In Rest APIs, when you write an API to get object details, you have to send a unique identifier to identify the record. This unique identifier can be a primary key (id in most of the cases). But we should not expose primary key in the API requests. So we can add a unique identifier “slug” in the database table to query on that. Slug field value should contain only letters, numbers, hyphens or underscores.

This article is about how to generate the slug value automatically as soon as you create a new record or update existing record.

Create a file

from django.utils.text import slugify

def generate_unique_slug(model_name, field, slug_field_name=None):
   origin_slug = slugify(field)
   unique_slug = origin_slug
   numb = 1
   if slug_field_name == 'code':
       while model_name.objects.filter(code=unique_slug).exists():
           unique_slug = '%s-%d' % (origin_slug, numb)
           numb += 1
       while model_name.objects.filter(slug=unique_slug).exists():
           unique_slug = '%s-%d' % (origin_slug, numb)
           numb += 1
   return unique_slug

You can modify this method based on “how your slug should be”.

In Admin, Import the slug generator file.

from slugGenerator import generate_unique_slug

Model Slug Fields

code = models.SlugField(blank=True)


Modify save() to generate slug when a new record is inserted as well as there is any change in the title.

class Course(models.Model):

   def __str__(self):

   name = models.CharField(max_length=100)
   code = models.SlugField(blank=True)
   is_active = models.BooleanField(default=False)

   def save(self, *args, **kwargs):
       if self.code:
           if slugify( != self.code:
               self.code = generate_unique_slug(Course,, "code")
           self.code = generate_unique_slug(Course,, "code")
       super(Course, self).save(*args, **kwargs)


Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top