class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField()
Is there a built-in way to get the slug field to autopopulate based on the title? Perhaps in the Admin and outside of the Admin.
Advertisement
Answer
for Admin in Django 1.0 and up, you’d need to use
prepopulated_fields = {'slug': ('title',), }
in your admin.py
Your key in the prepopulated_fields dictionary is the field you want filled, and the value is a tuple of fields you want concatenated.
Outside of admin, you can use the slugify
function in your views. In templates, you can use the |slugify
filter.
There is also this package which will take care of this automatically: https://pypi.python.org/pypi/django-autoslug