0

我希望所有表中的所有实例都有一个对象实例。一对一的主键字段看起来是一种很好的方法。就像下面的一个小例子。

from util.fields import BigAutoField,BigForeignKey
from django.db import models


class Common_document(models.Model):
    pk = models.OneToOneField("Type_object", primary_key=True, related_name="common_document_content_pk")
    author = models.ManyToManyField("Type_object", related_name = 'common_document_author',
                                 limit_choices_to = {"Common_document_author_author":"Type_object"} ,null = True,
                                 through = 'Common_document_author', blank = True)
    #....

class Common_document_author(models.Model):
    pk = models.OneToOneField("Type_object", primary_key=True, related_name="common_document_author_pk")
    document = BigForeignKey("Common_document", related_name='Common_document_author_document')
    author = BigForeignKey("Type_object", related_name='Common_document_author_author')


class Type_object(models.Model):
    id = BigAutoField(primary_key=True)
    #....
    # Some of the fields are m2m

但是,这给出了以下错误:

django.core.management.base.CommandError:一个或多个模型未验证:schema.common_document:中间模型 Common_document_author 对 Type_object 有多个外键,这是不明确的,是不允许的。

如果我注释掉 document_author 表中的 pk 字段,则会删除此错误。我猜错误的出现是因为 django 不确定要使用的女巫对象 FK。我该如何解决?有没有办法告诉django m2m 表中的哪个字段用于m2m 字段?

我可能不会这样做。m2m 表可能不需要对象实例,但我仍然想知道如何做到这一点。

4

1 回答 1

0

我想我不明白你的动机。为什么要使用外键作为主索引?当然,索引它,但主要?您也可以尝试将其名称从“pk”更改,我相信 Django 对名为“pk”的字段做出了假设。

于 2013-01-17T18:57:58.103 回答