Django中的单表继承 [英] Single Table Inheritance in Django

查看:107
本文介绍了Django中的单表继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否明确支持Django中的单表继承?最后我听说这个功能还在发展和辩论之中。



有没有可以使用图书馆/黑客来捕捉基本行为?我有一个层次结构混合不同的对象。具有Employee类的公司结构的规范示例,雇员类型的子类以及manager_id(parent_id)将是我正在解决的问题的良好近似。



在我的情况下,我想表达一个想法,即员工可以管理其他员工,同时由不同的员工进行管理。管理员和工作人员没有单独的课程,这使得它难以跨表传播。子类将代表员工的类型 - 程序员,会计师,销售人员等,并且独立于谁监督谁(好的,我猜这不再是一些典型的公司)。

解决方案

目前在Django - MTI(模型表继承)和ABC(抽象基类)中有两种继承形式。 / p>

我写了一个教程关于发生什么事情。



您还可以参考有关模型继承的官方文档


Is there explicit support for Single Table Inheritance in Django? Last I heard, the feature was still under development and debate.

Are there libraries/hacks I can use in the meantime to capture the basic behavior? I have a hierarchy that mixes different objects. The canonical example of a corporation structure with an Employee class, subclasses for types of employees, and a manager_id (parent_id) would be a good approximation of the problem I am solving.

In my case, I would like to represent the idea that an employee can manage other employees while being managed by a different employee. There are not separate classes for Manager and Worker, which makes this hard to spread across tables. Sub-classes would represent types of employees-programmers, accountants, sales, etc and would be independent of who supervises who (OK, I guess it's no longer a typical corporation in some respect).

解决方案

There are currently two forms of inheritance in Django - MTI (model table inheritance) and ABC (abstract base classes).

I wrote a tutorial on what's going on under the hood.

You can also reference the official docs on model inheritance.

这篇关于Django中的单表继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆