python
40 lines · 7 steps
Auto-generating unique slugs in Django
A Django model that derives a unique slug from its title and keeps it in sync whenever the title changes.
Explained by
highlit
1from django.db import models
2from django.utils.text import slugify
3
4
5class Article(models.Model):
6 title = models.CharField(max_length=200)
7 slug = models.SlugField(max_length=220, unique=True, editable=False)
8 body = models.TextField()
9 published_at = models.DateTimeField(null=True, blank=True)
10
11 class Meta:
12 ordering = ["-published_at"]
13
14 def __str__(self):
15 return self.title
16
17 def save(self, *args, **kwargs):
18 if not self.slug or self._title_changed():
19 self.slug = self._unique_slug()
20 super().save(*args, **kwargs)
21
22 def _title_changed(self):
23 if not self.pk:
24 return True
25 old_title = Article.objects.filter(pk=self.pk).values_list("title", flat=True).first()
26 return old_title != self.title
27
28 def _unique_slug(self):
29 base = slugify(self.title)[:200] or "article"
30 slug = base
31 siblings = Article.objects.exclude(pk=self.pk)
32 suffix = 2
33 while siblings.filter(slug=slug).exists():
34 slug = f"{base}-{suffix}"
35 suffix += 1
36 return slug
37
38 def get_absolute_url(self):
39 from django.urls import reverse
40 return reverse("article_detail", kwargs={"slug": self.slug})
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Overriding save lets a model compute derived fields before every write.
- 2Comparing against the stored row detects field changes without extra state.
- 3Appending an incrementing suffix guarantees uniqueness under a unique constraint.
Related explainers
python
from flask import Blueprint, jsonify from sqlalchemy import text from sqlalchemy.exc import SQLAlchemyError
Building a health check endpoint in Flask
health-check
blueprint
error-handling
Intermediate
6 steps
python
import pandas as pd import numpy as np
Cleaning a customer DataFrame with pandas
data-cleaning
regex
normalization
Intermediate
9 steps
python
from datetime import date, timedelta from typing import Annotated from fastapi import APIRouter, Depends, Query
Validating date ranges with FastAPI dependencies
dependency-injection
validation
pydantic
Intermediate
6 steps
python
from collections.abc import MutableMapping class CaseInsensitiveDict(MutableMapping):
Building a case-insensitive dict in Python
data structures
abstract base classes
dunder methods
Intermediate
8 steps
python
from fastapi import FastAPI, Request, status from fastapi.encoders import jsonable_encoder from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse
Redacting sensitive fields in FastAPI errors
validation
error-handling
security
Intermediate
7 steps
python
from datetime import timedelta from flask import Blueprint, current_app, make_response, redirect, request, url_for from itsdangerous import URLSafeTimedSerializer, BadSignature, SignatureExpired
How signed remember-me cookies work in Flask
authentication
signed-cookies
session-management
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/auto-generating-unique-slugs-in-django-explained-python-6ab2/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.