在Django / South HOWTO在DataMigration期间从不同的应用程序创建一个模型的实例 [英] In Django/South HOWTO create an instance of a model from a different app during DataMigration

查看:195
本文介绍了在Django / South HOWTO在DataMigration期间从不同的应用程序创建一个模型的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在app 问题中执行模型答案的数据迁移。在该脚本中有一个依赖关系,因此我需要创建一个 的实例。所以,我编码如下:

I need to perform a datamigration of a model Answer in app Question. In that script there is a dependency such that I need to create an instance of a model Chapter which is in the app Journal. So, I coded it as follows:

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)上抛出一个错误说,

But as expected this throws an error on " orm['journal.Chapter'].objects.get_or_create(content_object=blog)" saying that

django.core.exceptions.FieldError: Cannot resolve keyword 'content_object' into field.

这可能是因为content_object是一个GenericForeignKey,所以一些操作是不允许的。但是我也尝试其他替代方法来创建章节对象,如

This is presumably due to content_object being a GenericForeignKey so some operations are not allowed. But I also tried other alternatives for creating the "chapter" object like,

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']?)

所以在哪里我错了吗任何指针赞赏。谢谢。

So where am I going wrong? Any pointers appreciated. Thanks.

更新

所以,因为我以前的做法是失败我试过新的粘性在日记应用程序中,我的代码中的代码失败的模型,即,我决定为此创建一个数据迁移。我还确保在转发定义中指向的模型 - 冻结。现在这应该是直截了当的,我会想。我有我的转发代码如下 -

So since my earlier approach was failing I tried a new tack. The model whose instantiation was failing in my code above i.e. Chapter in the Journal app, I decided to create a datamigration for that instead. I also made sure to --freeze the models I am referring to in the forwards definition. Now this should have been straight forward, I would think. I have my forward code as follows -

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 )的实例,所有应用都应该出。但是我收到相同的错误。

I would have thought that now since I am creating instance of a model (Chapter) which exists in the subject app (Journal) everything should have worked out. But i am getting the same error.

TypeError: 'content_object' is an invalid keyword argument for this function

它在同一时刻失败,即content_object。如果可能有帮助,我将发布在模型定义之下。

It fails at the same point, namely, "content_object". I will post below the model definition if that might help.

class Chapter(models.Model):

    rank = models.IntegerField()

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

更新2
想添加所有在这些转发方式,即博客,章节,问题;在由南方的模式迁移创建的00n _ *。py文件中完全定义。

UPDATE 2 Wanted to add that all the models being touched in these forwards methods, namely - blog, chapter, questions; are fully defined in the 00n_*.py files created by South's schemamigration.

推荐答案

在得到Rob和南方人的帮助后&安培; Django用户组我能够解决这个问题。下面是我的转发数据迁移脚本的定义。

After getting help from Rob and folks on the South & Django user groups I was able to resolve this issue. Below is the definition of my forwards datamigration script.

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 / South HOWTO在DataMigration期间从不同的应用程序创建一个模型的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆