I added a slug field to my database and now need to go through and add those. I want to run a script that looks at the slug field in the database and if empty generates and saves. Here is what I thought was along the lines, but is not working.
from project.apps.tracks.models import *
def process_slug():
if not track.slug:
slug = slugify("%s - %s" % (track.name, track.artist))
else:
slug = ""
super(process_slug, track).save()
From your posted code it is not evident, that you are actually looping through all your
Trackobjects.One preferred place for such occasionally recurring commands would be under
management/commandsinside your application.Another way to implement would be to override your
Trackmodel’ssavemethod. It would check for emtpy slugs on every save (which is not performant).