ВАЖНО: этот вопрос больше не актуален.
При миграции Django 1.7 я пытаюсь программно создавать записи комментариев с помощью следующего кода:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
def create_genericcomment_from_bookingcomment(apps, schema_editor):
BookingComment = apps.get_model('booking', 'BookingComment')
Comment = apps.get_model('django_comments', 'Comment')
for comment in BookingComment.objects.all():
new = Comment(content_object=comment.booking)
new.save()
dependencies = [
('comments', '0001_initial'),
('django_comments', '__first__'),
]
operations = [
migrations.RunPython(create_genericcomment_from_bookingcomment),
]
И выдает ошибку: TypeError: 'content_object' is an invalid keyword argument for this function
Однако тот же код (то есть Comment(content_object=comment.booking)
) работает при выполнении в оболочке.
Я попытался создать пустую модель с помощью new = Comment()
, а затем вручную установить все необходимые поля, но, несмотря на то, что я установил поля content_type
и object_pk
соответственно, они content_type
фактически не были сохранены, и я получил django.db.utils.IntegrityError: null value in column "content_type_id" violates not-null constraint
Любая идея, как правильно создать модель с общим внешним ключом при миграции? Или любой обходной путь?