How to automatically generate slugs in Django Admin

There are several ways we can automatically populate the slug field, but I'm going to use the one I consider the simplest and probably the most used. I'd say that in 99.9% of cases, we'll want the slug to be the same as the title.

To do this, go to the Django admin and use the prepopulated_fields property. We specify that the slug field should be populated from the title field.

djangoshopping\<APP>\admin.py

class PostAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("title",)}

This is a tuple, and since it only has one element, the comma at the end is required.
If you don't include it, it won't be interpreted as a tuple and you'll get an error.
If we included two fields, the comma after the second one wouldn't be required.

I agree to receive announcements of interest about this Blog.

We will see how we can generate SLUGs for entities in Django Admin.

- Andrés Cruz

En español