EazyC
EazyC

Reputation: 819

Django Slug field only generated when new models made, not old migrated models

I have a django model object that has a slug field, but this slug only gets set once, when the model is created. The problem is that I have migrated over a large database of these models from a previous version of the app that did not have a slug field so the slug field is now null on all the old objects. How can I rewrite my model or any part of my views to automatically populate the slug field of all of my old model objects, not just the new ones being created?

class NewLink(models.Model):
     newlinktag = models.ForeignKey('pageinfo', related_name='newlink_newlinktag') #tags link to which username it belongs to
     linkcomment = models.CharField(max_length=512) #comment to go along with post
     url = models.URLField(max_length = 1024) #actual submitted link, need max length so we don't get hacked
     slug = models.SlugField(('slug'), max_length=512, blank=True)

    def save(self, *args, **kwargs):
         self.slug = slugify(self.linkcomment)
         super(newlink, self).save(*args, **kwargs)

     def __unicode__(self):
         return self.url

Upvotes: 1

Views: 348

Answers (1)

viktor.svirskyy
viktor.svirskyy

Reputation: 479

You need to write you convertor(updater) into the migration file (in forward case). And if you want you can implement backward case

Example here http://concentricsky.com/blog/2014/jan/data-migrations-south-and-django

Upvotes: 1

Related Questions