简体   繁体   English

Django继承和子类属性

[英]Django inheritance and subclass attributes

With a configuration such as this: 使用这样的配置:

class A (models.Model):
    common_attribute = models.IntegerField()

class B (A):
    subclass_attribute = models.IntegerField()

class C (models.Model)
    a = ForeignKey(A)

... if an instance of C contains an instance of B, Django seems to treat this as an instance of A and I can't access casubclass_attribute. ...如果C的实例包含B的实例,则Django似乎将其视为A的实例,因此我无法访问casubclass_attribute。 Is there a way around this? 有没有解决的办法? I don't want to use abstract inheritance because of the difficulties with ForeignKeys - I want C to support any subclass of A. 由于ForeignKeys的困难,我不想使用抽象继承-我希望C支持A的任何子类。

You can circumvent the difficulties with ForeignKey by using django-polymorphic . 您可以使用django-polymorphic来解决与ForeignKey的困难。

Django Polymorphic allows you to query the base class objects but retrieves the child class instances: 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"> ]

To use django polymorphic you only need to declare your models with Polymorphic Model as base class: 要使用django多态,您只需要使用Polymorphic Model作为基类声明模型:

from django.db import models
from polymorphic 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)

Foreign keys will also return the child class instances, which I think is exactly what you want. 外键还将返回子类实例,我认为这正是您想要的。

# 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)> ]

Take into account that these queries will be slightly less performant . 考虑到这些查询的性能稍差一些

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django:“模型”子类继承 - Django: 'Model' subclass inheritance Django模型继承:删除子类保持超类 - Django model inheritance: Delete subclass keep superclass 在继承中查询特定的子类(子级)将呈现基类(父级)属性以及子类属性 - Querying for a particular subclass(child) in an inheritance will render the base class(parent) attributes in addition to the subclass attributes Django子类(模型)未实现超类的属性 - Django subclass (model) not implementing attributes of superclass Django Model Inheritance - 实例属性设置不正确 - Django Model Inheritance - Instance attributes not set properly Django模型继承-查询时实例化为super的子类 - Django model inheritance - Subclass instantiated as super when querying 如何子类化 Django TextChoices 以添加其他属性? - How can I subclass Django TextChoices to add additional attributes? Django-tables2子类错误(类属性未传递给实例化对象) - Django-tables2 subclass error (class attributes not passed to instantiated object) 使用基类分配子类属性时出现Python类继承问题 - Python class inheritance issue occurs when using base class to assign attributes of subclass Python Meta类和继承-无法识别的属性(django-tables2) - Python Meta class and inheritance — attributes not recognized (django-tables2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM