我希望所有表中的所有实例都有一个对象实例。一对一主键字段看起来是一种很好的方法。就像下面的一个小例子。
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表可能不需要有一个对象实例,但我仍然想知道如何做到这一点。
答案 0 :(得分:0)
我想我不理解你的动机。为什么要使用外键作为主索引?当然,索引它,但主要?您也可以尝试从'pk'更改其名称,我相信Django会对名为'pk'的字段做出假设。