django - 在 Django/South 如何在数据迁移期间从不同的应用程序创建模型的实例

标签 django instantiation data-migration django-south generic-foreign-key

我需要在应用程序 问题 中执行模型 Answer 的数据迁移。在该脚本中有一个依赖项,因此我需要创建一个模型 Chapter 的实例,该实例位于应用程序 Journal 中。所以,我将其编码如下:

def forwards(self, orm):
    for answer_object in orm.Answer.objects.all():

        #This Works.
        blog, is_created = orm['blog.Post'].objects.get_or_create(title=answer_object.answer[:100])
        blog.save()

        #This DOES NOT work
        chapter, is_created = orm['journal.Chapter'].objects.get_or_create(content_object=blog)
        chapter.save()
        #cleanup task, not relevant to this question below
        answer_object.chapter_ptr = chapter
        answer_object.save()

但正如预期的那样,这会在“orm['journal.Chapter'].objects.get_or_create(content_object=blog)”上引发错误,说
django.core.exceptions.FieldError: Cannot resolve keyword 'content_object' into field.

这大概是因为 content_object 是一个 GenericForeignKey 所以一些操作是不允许的。但我也尝试了其他替代方法来创建“章节”对象,例如,
chapter = orm['journal.Chapter'](content_object=blog)
ERROR > TypeError: 'content_object' is an invalid keyword argument for this function


chapter = orm.journal.Chapter(content_object=blog)
 ERROR > AttributeError: The model 'journal' from the app 'questions' is not available in this migration. (Did you use orm.ModelName, not orm['app.ModelName']?)

那么我哪里出错了?任何指针表示赞赏。谢谢。

更新

因此,由于我之前的方法失败了,我尝试了一种新方法。在我上面的代码中实例化失败的模型,即 Journal 应用程序中的 Chapter ,我决定为此创建一个数据迁移。我还确保将我在 --freeze 定义中所指的模型设为 forwards。现在这应该是直截了当的,我想。我的转发代码如下 -
def forwards(self, orm):

    for answer_object in orm['questions.Answer'].objects.all():

        #Works, AGAIN!
        blog, is_created = orm['blog.Post'].objects.get_or_create(title=answer_object.answer[:100])
        blog.save()

        # DOES NOT WORK, AGAIN!
        chapter = orm.Chapter(rank=1, content_object=blog)       
        chapter.save()

我会认为现在因为我正在创建一个模型的实例( Chapter )它存在于主题应用程序( Journal )中,一切都应该已经解决了。但我遇到了同样的错误。
TypeError: 'content_object' is an invalid keyword argument for this function

它在同一点失败,即“content_object”。如果可能有帮助,我将在模型定义下方发布。
class Chapter(models.Model):

    rank = models.IntegerField()

    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()

更新 2
想补充一下这些转发方法中涉及的所有模型,即 - 博客、章节、问题;完全在 South 的 schemamigration 创建的 00n_*.py 文件中定义。

最佳答案

在从 Rob 和 South & Django 用户组的人们那里得到帮助后,我能够解决这个问题。以下是我的 forwards 的定义数据迁移脚本。

def forwards(self, orm):

    for answer_object in orm['questions.Answer'].objects.all():


        blog, is_created = orm['blog.Post'].objects.get_or_create(title=answer_object.answer[:100])
        blog.save()

        #I have to manually lookup the content_type ans set it in the chapter creation.
        ct = orm['contenttypes.ContentType'].objects.get(app_label="blog", model="post")    
        chapter = orm.Chapter(rank=1, content_type=ct, object_id=blog.id)

        chapter.save()

关于django - 在 Django/South 如何在数据迁移期间从不同的应用程序创建模型的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6976879/

相关文章:

django - Heroku 没有获取更新的 django-bouncer 包

c++ - 显式模板实例化问题

oracle - 在 oracle 中的不同服务器之间移动数据

python - 自定义 404 模板上的额外变量

python - Django 运行服务器错误 : unsupported operand type(s) for +=: 'NoneType' and 'str'

c# - 使用反射在 C# 中创建没有默认构造函数的类型实例

block 内声明的对象的 C++ 持久性,内存泄漏的可能性?

azure - 连接到 Azure 中的数据迁移的源服务器时出现以下错误

database - 如何使用 Django 中的 South 将数据从一个模型迁移到另一个模型?

ios - 在 iOS 应用程序上获取 Django 当前用户