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

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

问题描述

Django 中是否明确支持单表继承?最后我听说,该功能仍在开发和辩论中.

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

在此期间,我可以使用哪些库/技巧来捕捉基本行为?我有一个混合不同对象的层次结构.具有 Employee 类、雇员类型的子类和 manager_id (parent_id) 的公司结构的规范示例将是我正在解决的问题的一个很好的近似.

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.

就我而言,我想代表这样一种想法,即一名员工可以管理其他员工,同时由另一名员工管理.Manager 和 Worker 没有单独的类,这使得这很难跨表传播.子类将代表员工类型——程序员、会计师、销售人员等,并且独立于谁监督谁(好吧,我想它在某些方面不再是典型的公司).

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).

推荐答案

Django 中目前有两种继承形式——MTI(模型表继承)和 ABC(抽象基类).

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.

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

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

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