我有一个Django应用程序,我想在其中将ForeignKey中的字段更改为ManyToManyField.我想保留我的旧数据.最简单/最好的流程是什么?如果重要,我使用sqlite3作为我的数据库后端.
如果我对问题的总结不清楚,这是一个例子.说我有两个型号:
class Author(models.Model):
author = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author)
title = models.CharField(max_length=100)
Run Code Online (Sandbox Code Playgroud)
假设我的数据库中有很多数据.现在,我想更改Book模型,如下所示:
class Book(models.Model):
author = models.ManyToManyField(Author)
title = models.CharField(max_length=100)
Run Code Online (Sandbox Code Playgroud)
我不想"丢失"我之前的所有数据.
完成此任务的最佳/最简单方法是什么?
肯
我正在尝试向我之前创建的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
我有一个看起来像这样的模型:
class Assignment(models.Model):
"""An assignment covers a range of years,
has multiple coders and one specific task"""
title = models.CharField(blank=True, max_length=100)
start_date = models.DateField(default=date.today)
end_date = models.DateField(default=date.today)
coders = models.ManyToManyField(User, related_name='assignments')
Run Code Online (Sandbox Code Playgroud)
我想改变这个模型(它有数据已经在生产中),使它看起来像这样:
class Assignment(models.Model):
"""An assignment covers a range of years,
has multiple coders and one specific task"""
title = models.CharField(blank=True, max_length=100)
start_date = models.DateField(default=date.today)
end_date = models.DateField(default=date.today)
coders = models.ManyToManyField(User, through = 'AssignmentProgress')
class AssignmentProgress(models.Model):
"""The progress a particular coder has made with an assignment"""
assignment = models.ForeignKey(Assignment, related_name="assignment_progress") …Run Code Online (Sandbox Code Playgroud)