7

アプリQuestionでモデルAnswerのデータ移行を実行する必要があります。そのスクリプトには、アプリJournalにあるモデルChapterのインスタンスを作成する必要があるような依存関係があります。ということで、以下のようにコーディングしました。

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でインスタンス化が失敗していたモデルは、代わりにそのためのデータ移行を作成することにしました。また、定義で参照しているモデルも確認しました。今、これは簡単なことだったはずです。私は次のように転送コードを持っています ---freezeforwards

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()

問題のアプリ ( Journal ) に存在するモデル ( Chapter )のインスタンスを作成しているので、すべてがうまくいくはずだと思っていたでしょう。しかし、私は同じエラーが発生しています。

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 これらの forwards メソッド、つまり、ブログ、章、質問で触れられているすべてのモデルを追加したかった。South の schemamigration によって作成された 00n_*.py ファイルで完全に定義されています。

4

4 に答える 4

8

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()
于 2011-08-13T19:03:44.620 に答える
1

これは、ここの前にある種の回答がありますDjango South:複数のアプリのスキーマ移行の作成

基本的に、southは現在複数のアプリケーションをサポートしていません。

アプリケーションをそれ以上に絡ませたくない場合は、db.executeで生のSQLを使用して簡単に修正します。

ブログとジャーナルは非常に相互に関連しているようです。別々のアプリケーションでそれらを使用してもよろしいですか?

于 2011-08-08T01:05:40.387 に答える
0

これはチャンツの答えに対するコメントで、私にとってはうまくいきました(これをコメントとして投稿する担当者はいません)

他の時間を節約するために、移行を作成するときに contenttype アプリをフリーズする必要もあります。

python manage.py datamigration yourapp migrationname --freeze contenttypes
于 2014-10-20T22:06:30.407 に答える
0

manage.py 経由で移行を作成するには、引数として --freeze=other_app を渡し、この other_app のモデル定義を移行自体に追加します。

于 2014-01-31T16:26:44.657 に答える