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