我的Blog
模型有AutoSlugField
which usesBlog.__unicode__()
方法。
数据迁移后,所有Blog
实例都将 slug 设置为blog-object-<number>
而不是<year>-<month>-<day>
. 似乎定义Blog.__unicode__()
被忽略了。
我怎样才能正确迁移Blog
模型?
模型字段.py:
class AutoSlugField(models.CharField):
def pre_save(self, blog, *args, **kwargs):
return slugify(unicode(blog))
模型.py:
class Blog(models.Model):
title = models.CharField(max_length=255)
created = models.DateTimeField(auto_now_add=True)
slug = AutoSlugField(max_length=50)
def __unicode__(self):
return self.created.strftime('%Y-%m-%d')
移民:
from south.v2 import DataMigration
class Migration(DataMigration):
def forwards(self, orm):
for blog in orm.Blog.objects.all():
blog.title = blog.title.replace('django', 'Django')
blog.save()