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 django.db.models import Prefetch, Count from django.views.generic import ListView from .models import Article, Comment
Building an efficient article list in Django
orm
query-optimization
prefetching
Intermediate
6 steps
python
from functools import lru_cache import math
Memoizing number theory with lru_cache
memoization
number-theory
caching
Intermediate
8 steps
python
import re from collections import defaultdict from pathlib import Path
Summarizing log files by date in Python
regex
parsing
aggregation
Intermediate
7 steps
python
import threading import logging logger = logging.getLogger(__name__)
A self-rescheduling periodic task in Python
threading
scheduling
concurrency
Intermediate
6 steps
python
from collections import ChainMap from functools import reduce from typing import Any, Iterable, Mapping
Five ways to merge dicts in Python
dictionaries
merging
recursion
Intermediate
8 steps
python
from fastapi import Depends, FastAPI, HTTPException from sqlalchemy import create_engine from sqlalchemy.orm import Session, sessionmaker
Wiring SQLAlchemy sessions into FastAPI
dependency-injection
orm
sessions
Intermediate
7 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.