django - 将外键用于抽象 class 有哪些替代方法?

django - What are the alternatives to having a ForeignKey to an abstract class?

我想在 Django 中有一个抽象的 Company 模型,并根据所涉及的公司类型对其进行扩展:

class Company(models.Model):
    name = models.CharField(max_length=100)
    address = models.CharField(max_length=100)

    class Meta:
        abstract = True

class Buyer(Company):
    # Buyer fields..
    pass

class Seller(Company):
    # Seller fields...
    pass

系统上的每个用户都与一家公司相关联,因此我想将以下内容添加到用户配置文件中:

company = models.ForeignKey('Company')

但这给出了可怕的错误:

main.Profile.company: (fields.E300) Field defines a relation with model 'Company', which is either not installed, or is abstract.

所以我想我想做的事无法完成。我看到 contenttypes framework could be used for this purpose, as answered in this 问题。我的问题是我不希望 company 字段指向 任何 模型,而只是指向 Company 模型的子类。

还有什么我可以用于此目的的吗?

ForeignKey 不能直接引用抽象模型的原因是从抽象模型继承的各个模型实际上在数据库中有自己的 table。

外键只是引用相关 table 中的 id 的整数,因此如果外键与抽象模型相关,则会产生歧义。例如,可能有一个 BuyerSeller 实例,每个实例的 id 都是 1,而管理器不知道要加载哪个实例。

使用 generic relation 还可以记住您在关系中谈论的模型,从而解决了这个问题。

它不需要任何额外的模型,它只是使用一个额外的列。

例子-

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey

class Foo(models.Model):
  company_type = models.ForeignKey(ContentType)
  company_id = models.PositiveIntegerField()
  company = GenericForeignKey('company_type', 'company_id')

然后-

>>> seller = Seller.objects.create()
>>> buyer = Buyer.objects.create()
>>> 
>>> foo1 = Foo.objects.create(company = seller) 
>>> foo2 = Foo.objects.create(company = buyer) 
>>> 
>>> foo1.company 
<Seller: Seller object>
>>> foo2.company 
<Buyer: Buyer object>