Python Django ReverseSingleRelatedObjectDescriptor.\uuuu设置\uuuuuu值错误

Python Django ReverseSingleRelatedObjectDescriptor.\uuuu设置\uuuuuu值错误,python,django,django-migrations,django-contenttypes,Python,Django,Django Migrations,Django Contenttypes,我正在创建一个自定义数据迁移,以基于两个不同模型中的现有条目,在数据库中自动创建GenericRelation条目 示例模型。py: ... class Place content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id'

我正在创建一个自定义数据迁移,以基于两个不同模型中的现有条目,在数据库中自动创建GenericRelation条目

示例模型。py:

...
class Place
   content_type = models.ForeignKey(ContentType)
   object_id = models.PositiveIntegerField()
   content_object = generic.GenericForeignKey('content_type', 'object_id')


class Restaurant
   name = models.CharField(max_length=60)
   location = models.CharField(max_length=60)


class House
   location = models.CharField(max_length=60)
# -*- coding: utf-8 -*-
from django.contrib.contenttypes.models import ContentType
from django.db import models, migrations

def forwards_func(apps, schema_editor):
    Restaurant = apps.get_model("simpleapp", "Restaurant")
    House = apps.get_model("simpleapp", "House")
    Place = apps.get_model("simpleapp", "Place")

    db_alias = schema_editor.connection.alias

    content_type = ContentType.objects.using(db_alias).get(
        app_label="simpleapp",
        model="restaurant"
    )

    for restaurant in Restaurant.objects.using(db_alias).all():
        Place.objects.using(db_alias).create(
            content_type=content_type,
            object_id=restaurant.id)

    content_type = ContentType.objects.using(db_alias).get(
        app_label="simpleapp",
        model="house"
    )
    for house in House.objects.using(db_alias).all():
        Place.objects.using(db_alias).create(
            content_type=content_type,
            object_id=house.id)


class Migration(migrations.Migration):

    dependencies = [
        ('simpleapp', '0010_place')
    ]

    operations = [
        migrations.RunPython(
            forwards_func,
        ),
    ]
示例0011\u place\u data.py:

...
class Place
   content_type = models.ForeignKey(ContentType)
   object_id = models.PositiveIntegerField()
   content_object = generic.GenericForeignKey('content_type', 'object_id')


class Restaurant
   name = models.CharField(max_length=60)
   location = models.CharField(max_length=60)


class House
   location = models.CharField(max_length=60)
# -*- coding: utf-8 -*-
from django.contrib.contenttypes.models import ContentType
from django.db import models, migrations

def forwards_func(apps, schema_editor):
    Restaurant = apps.get_model("simpleapp", "Restaurant")
    House = apps.get_model("simpleapp", "House")
    Place = apps.get_model("simpleapp", "Place")

    db_alias = schema_editor.connection.alias

    content_type = ContentType.objects.using(db_alias).get(
        app_label="simpleapp",
        model="restaurant"
    )

    for restaurant in Restaurant.objects.using(db_alias).all():
        Place.objects.using(db_alias).create(
            content_type=content_type,
            object_id=restaurant.id)

    content_type = ContentType.objects.using(db_alias).get(
        app_label="simpleapp",
        model="house"
    )
    for house in House.objects.using(db_alias).all():
        Place.objects.using(db_alias).create(
            content_type=content_type,
            object_id=house.id)


class Migration(migrations.Migration):

    dependencies = [
        ('simpleapp', '0010_place')
    ]

    operations = [
        migrations.RunPython(
            forwards_func,
        ),
    ]
当我运行这个(Django 1.7.4)时,我得到

首先应该提出这个例外吗?这是Django中的错误还是我做错了

下面的解决方案实际上不起作用;它只是没有运行forwards_func,因此没有错误。新的解决方案欢迎:


我能用计算机解决这个问题

这还修复了这些类型迁移(引用ContentType表的数据迁移)带来的一些其他问题

我的理解是,本质上,问题是由于性能原因,ContentType表直到迁移的最后才被创建。这意味着我实际上并没有检索关系模块正在检查的同一类型ContentType对象

解决方案是将这种类型的数据迁移作为回调运行:

# -*- coding: utf-8 -*-
from django.db.models.signals import post_migrate
from django.contrib.contenttypes.models import ContentType
from django.db import models, migrations

def forwards_func(apps, schema_editor):
    Restaurant = apps.get_model("simpleapp", "Restaurant")
    House = apps.get_model("simpleapp", "House")
    Place = apps.get_model("simpleapp", "Place")

    db_alias = schema_editor.connection.alias

    def add_stuffs(*args, **kwargs)
        content_type = ContentType.objects.using(db_alias).get(
            app_label="simpleapp",
            model="restaurant"
        )

        for restaurant in Restaurant.objects.using(db_alias).all():
            Place.objects.using(db_alias).create(
                content_type=content_type,
                object_id=restaurant.id)

        content_type = ContentType.objects.using(db_alias).get(
            app_label="simpleapp",
            model="house"
        )
        for house in House.objects.using(db_alias).all():
            Place.objects.using(db_alias).create(
               content_type=content_type,
               object_id=house.id)

    post_migrate.connect(add_stuffs)


class Migration(migrations.Migration):

    dependencies = [
        ('simpleapp', '0010_place')
    ]

    operations = [
        migrations.RunPython(
            forwards_func,
        ),
    ]

看起来它和这个Django有点关系

在迁移过程中,我尝试将权限重新分配给用户组时也出现了类似的错误。为了修复它,我必须手动发出post_migrate信号,如上所述

以下是我的迁移代码示例:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations
from django.core.management.sql import emit_post_migrate_signal


def trigger_post_migrate(db_alias):
    try:
        # Django 1.9
        emit_post_migrate_signal(2, False, db_alias)
    except TypeError:
        # Django < 1.9
        try:
            # Django 1.8
            emit_post_migrate_signal(2, False, 'default', db_alias)
        except TypeError:
            # Django < 1.8
            emit_post_migrate_signal([], 2, False, 'default', db_alias)


def up(apps, schema_editor):
    # trigger post migrate to make sure all permissions and contenttypes are in place
    trigger_post_migrate(schema_editor.connection.alias)

    # migration code here...


class Migration(migrations.Migration):
    dependencies = [
        ('accounts', '0008_auto_20160712_0608'),
    ]

    operations = [
        migrations.RunPython(up),
    ]
#-*-编码:utf-8-*-
从未来导入unicode文字
从django.db导入迁移
从django.core.management.sql导入发出\u post\u migrate\u信号
def触发器后迁移(db\U别名):
尝试:
#Django 1.9
发出后迁移信号(2,False,db\u别名)
除类型错误外:
#Django<1.9
尝试:
#Django 1.8
发出后迁移信号(2,False,“default”,db\u别名)
除类型错误外:
#Django<1.8
发出后迁移信号([],2,False,“default”,db\u别名)
def up(应用程序、架构编辑器):
#触发后迁移以确保所有权限和内容类型都已就位
触发器\u post\u迁移(schema\u editor.connection.alias)
#迁移代码在这里。。。
类迁移(migrations.Migration):
依赖项=[
(“账户”,“0008自动”20160712“0608”),
]
操作=[
运行Python(向上),
]

我不明白你在评论什么。请出示完整的追踪结果好吗?