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 fastapi import FastAPI, WebSocket, WebSocketDisconnect app = FastAPI()
Building a WebSocket chat with FastAPI
websockets
broadcast
connection-management
Intermediate
9 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
python
import random from typing import Iterator, List
How reservoir sampling picks k items
reservoir-sampling
streaming
randomness
Intermediate
5 steps
python
import secrets from django.contrib.auth import authenticate, login from django.core.cache import cache
Two-factor login with OTP in Django
two-factor-auth
one-time-passwords
caching
Intermediate
9 steps
python
import re from functools import total_ordering from typing import Optional
Parsing and comparing semantic versions
regex
operator-overloading
sorting
Intermediate
7 steps
python
from typing import Any, Sequence, Mapping def render_markdown_table(
Rendering an aligned Markdown table in Python
string formatting
data transformation
closures
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.