no_freedom no_freedom - 2 months ago 22
Python Question

Generate slug field in existing table

I have table with data. Is it possible slug field automatically generated on existing table? Or is there any other alternative? Thanks
Here is my table

enter image description here

Answer

Using the slugify template filter, you can write a script, or loop through the objects in the shell.

>>> from django.template.defaultfilters import slugify
>>> for obj in MyModel.objects.all():
...     obj.slug = slugify(obj.title)
...     obj.save()