我有这个型号:
class BaseModel(models.Model):
....
class Meta:
abstract = True
class ModelA(BaseModel):
....
class ModelB(BaseModel):
....
class MyExtModel(models.Model)
myfield = models.ForeignKey(BaseModel)
但这不正确,因为我BaseModel
喜欢Abstract
。事实上,当我尝试makemigration
命令时,我有一个错误。
错误是:
ERRORS:
myapp.MyExtModel.myfield: (fields.E300) Field defines a relation with model 'BaseModel', which is either not installed, or is abstract.
有没有办法使用抽象基础模型?
我也尝试过使用:
myfield = models.ForeignKey(BaseModel, related_name="%(app_label)s_%(class)s_related")
答案 0 :(得分:8)
在Django中无法将外键安装到抽象模型中。 但是,您可以将外键安装到非抽象基类。唯一的限制是反向外键关系将返回基类实例。 您可以使用django-polymorphic来规避此限制。
Django Polymorphic允许您查询基类对象但检索子类实例:
>>> Project.objects.create(topic="Department Party")
>>> ArtProject.objects.create(topic="Painting with Tim", artist="T. Turner")
>>> ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
>>> Project.objects.all()
[ <Project: id 1, topic "Department Party">,
<ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">,
<ResearchProject: id 3, topic "Swallow Aerodynamics", supervisor "Dr. Winter"> ]
要使用django polymorphic,您只需要将多态模型声明为基类:
from django.db import models
from polymorphic.models import PolymorphicModel
class ModelA(PolymorphicModel):
field1 = models.CharField(max_length=10)
class ModelB(ModelA):
field2 = models.CharField(max_length=10)
class ModelC(ModelB):
field3 = models.CharField(max_length=10)
外键也将返回子类实例,这是我需要的假设:
# The model holding the relation may be any kind of model, polymorphic or not
class RelatingModel(models.Model):
many2many = models.ManyToManyField('ModelA') # ManyToMany relation to a polymorphic model
>>> o=RelatingModel.objects.create()
>>> o.many2many.add(ModelA.objects.get(id=1))
>>> o.many2many.add(ModelB.objects.get(id=2))
>>> o.many2many.add(ModelC.objects.get(id=3))
>>> o.many2many.all()
[ <ModelA: id 1, field1 (CharField)>,
<ModelB: id 2, field1 (CharField), field2 (CharField)>,
<ModelC: id 3, field1 (CharField), field2 (CharField), field3 (CharField)> ]
考虑到这些查询将是slightly less performant。
答案 1 :(得分:4)
当我遇到类似的情况时,我必须将ForeignKeys设为不同的模型,我选择使用GenericForeignKey
,您可以在此查看官方文档:Django ContentTypes: Generic Relations
文档很好地解释了如何使用它:
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
class TaggedItem(models.Model):
tag = models.SlugField()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
def __str__(self): # __unicode__ on Python 2
return self.tag
字段 content_type 存储通用外键指向的模型
字段 object_id 存储外键的ID,
这不是最好的解决方案,但它可以节省我的一些项目
使用它的例子:
from django.contrib.auth.models import User
guido = User.objects.get(username='Guido')
t = TaggedItem(content_object=guido, tag='bdfl')
t.save()
t.content_object
<User: Guido>
答案 2 :(得分:1)
除了GenericForeignKey
的好回答,我不熟悉,有时(有时候,只要有可能),通过与您的&一对一关系来简化您的模型是值得的。 #39;基础&#39;模型。
之后使外键管理更容易。如果我记得很清楚,抽象类上的外键是不可能的。