Django Autoslug
Django-autoslug is a reusable Django library that provides an improved slug field which can automatically: populate itself from another field, preserve uniqueness of the value, and use custom slugify() functions for better internationalization (i18n). The current version is 1.9.9. It maintains an active release cadence with regular updates and compatibility improvements for newer Django and Python versions.
Warnings
- breaking Version 1.9.9 removed support for end-of-life Python & Django versions. Ensure your environment meets the new requirements (Python 3.7+ or PyPy, Django 3.2+).
- breaking In version 1.9.8, the import path for `FieldDoesNotExist` was moved for compatibility with Django 3.1+. This change may cause import errors in projects using older Django versions with `django-autoslug` 1.9.8 or later.
- breaking Version 1.9.0 introduced significant backwards incompatible changes, including limiting supported Python/Django versions (initially Python 2.7, 3.5, PyPy; Django 1.7.10+). Additionally, `modeltranslation` support was turned off by default.
- gotcha While `django-autoslug` supports Unicode, it relies on `Unidecode` for robust transliteration. `Unidecode` is not installed as a direct dependency, so if you're dealing with non-ASCII characters, you may encounter issues unless you install `Unidecode` manually (`pip install Unidecode`).
- gotcha Always declare the `AutoSlugField` *after* any model fields it references (via `populate_from` or `unique_with`). This ensures that the referenced fields are already processed when the slug is generated.
- gotcha Automatically updating slugs (especially if `always_update=True` is used) can lead to broken external links or SEO issues if the source fields are frequently changed. Slugs used in public URLs should generally remain stable.
- gotcha Versions prior to 1.9.9 could generate slugs that end in a dash or underscore in certain situations.
Install
-
pip install django-autoslug
Imports
- AutoSlugField
from autoslug import AutoSlugField
Quickstart
from django.db import models
from autoslug import AutoSlugField
class Article(models.Model):
title = models.CharField(max_length=200)
# The slug will be populated from the 'title' field
slug = AutoSlugField(populate_from='title', unique=True)
def __str__(self):
return self.title