我一直在寻找南方网站,谷歌和SO的答案,但找不到一个简单的方法来做到这一点.
我想用South重命名一个Django模型.说你有以下内容:
class Foo(models.Model):
name = models.CharField()
class FooTwo(models.Model):
name = models.CharField()
foo = models.ForeignKey(Foo)
Run Code Online (Sandbox Code Playgroud)
并且你想将Foo转换为Bar,即
class Bar(models.Model):
name = models.CharField()
class FooTwo(models.Model):
name = models.CharField()
foo = models.ForeignKey(Bar)
Run Code Online (Sandbox Code Playgroud)
为了简单起见,我只是尝试将名称从更改Foo
为Bar
,但暂时忽略该foo
成员FooTwo
.
使用南方最简单的方法是什么?
db.rename_table('city_citystate', 'geo_citystate')
,但在这种情况下我不确定如何修复外键.我无法在文档或在线中找到对特定问题的引用.
我有很多关系.
class Books(models.Model):
name = models.CharField(max_length=100)
class Authors(models.Model):
name = models.CharField(max_length=100)
books = models.ManyToManyField(Books)
Run Code Online (Sandbox Code Playgroud)
这有迁移和数据.现在我需要使用through选项,以便在表中添加一个包含多对多关系的额外字段.
class Authorship(models.Model):
book = models.ForeignKey(Books)
author = models.ForeignKey(Authors)
ordering = models.PositiveIntegerField(default=1)
class Authors(models.Model):
name = models.CharField(max_length=100)
books = models.ManyToManyField(Books, through=Authorship)
Run Code Online (Sandbox Code Playgroud)
当我运行迁移时,django会为Authorship
模型创建新的迁移.我尝试通过ordering
在Authorship
表中添加列并在表中更改books
列来手动创建迁移文件,Authors
但是我遇到了一些迁移问题.
operations = [
migrations.AddField(
model_name='authorship',
name='ordering',
field=models.PositiveIntegerField(default=1),
),
migrations.AlterField(
model_name='authors',
name='books',
field=models.ManyToManyField(to='app_name.Books', through='app_name.Authorship'),
),
]
Run Code Online (Sandbox Code Playgroud)
在尝试迁移时,KeyError: ('app_name', u'authorship')
我认为还有其他因素会受到影响,从而导致错误.
我错过了什么?有没有其他方法可以解决这个问题?
我正在尝试向我之前创建的ManyToMany字段添加订单.我基本上想订购图片集中的图片.我在Django 1.7上运行,所以没有更多的南迁移(我试图遵循这个教程:http://mounirmesselmeni.github.io/2013/07/28/migrate-django-manytomany-field-to-manytomany-through -with -南/)
这是我的"通过"关系:
class CollectionPictures(models.Model):
picture = models.ForeignKey(
Picture,
verbose_name=u'Picture',
help_text=u'Picture is included in this collection.',
)
collection = models.ForeignKey(
Collection,
verbose_name=u'Collection',
help_text=u'Picture is included in this collection',
)
order = models.IntegerField(
verbose_name=u'Order',
help_text=u'What order to display this picture within the collection.',
max_length=255
)
class Meta:
verbose_name = u"Collection Picture"
verbose_name_plural = u"Collection Pictures"
ordering = ['order', ]
def __unicode__(self):
return self.picture.name + " is displayed in " + self.collection.name + (
" in …
Run Code Online (Sandbox Code Playgroud) migration django manytomanyfield django-1.7 django-migrations