本文介绍了从具有相同父级但不同子级的抽象类的多重继承?詹戈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了几个线程,并且知道django肯定可以有多个抽象类.但是我看到的几乎所有样本都是...

I have read few threads and know that for sure django can have multiple abstract classes. But pretty much all the samples I saw are...

class AbsOne(models.Model):
    pass
class AbsTwo(models.Model):
    pass

class AbsThree(AbsOne, AbsTwo):
    pass

但是如果我有类似的东西...

but what if I have something like...

class AbsOne(models.Model):
    pass

class AbsTwo(AbsOne):  // this actually inheritance AbsOne
    pass
class AbsThree(AbsOne):  // this inheritance AbsOne
    pass

如果我都需要继承AbsTwo, AbsThree,但是这两个也都继承到同一个父级,该怎么办?

What if I need to inheritance both AbsTwo, AbsThree but these two are also inheritance to the same parent.

class AbsFour(AbsTwo, AbsThree):
    pass

这是可行的,没有任何冲突或多余的字段吗?

Is this doable without any conflict or extra fields?

谢谢.

推荐答案

https://docs.djangoproject.com/zh/1.11/topics/db/models/#multiple-inheritance

这是可能的,但是它有一些限制(例如(覆盖父类和元类中的字段)和Django ORM),或者类的模型方式在体系结构上与常规python继承有所不同,请仔细阅读文档中提到的内容并尝试为了简单起见.

It's possible but it has some restrictions like ( overriding fields in parent class and meta classes ) and in Django ORM or the model way of classes is a bit different in architecture than regular python inheritance, read carefully what the documentation mentions and try to keep it simple.

添加了另一个引号:)

这篇关于从具有相同父级但不同子级的抽象类的多重继承?詹戈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 22:56