Django multi-table 继承不同于 Postgres table 继承

Django multi-table inheritance different from Postgres table inheritance

所以我正在研究 Django 的多table 继承,以及它与 Postgres 的 table 继承有何不同。

假设我有以下型号:

models.py

class Mayor(models.Model):
    name = models.CharField(max_length=255)


class City(models.Model)
    name = models.CharField(max_length=255)
    mayor = models.ForeignKey(Mayor, on_delete=models.CASCADE)


class Capital(City):
    embassy = models.BooleanField(default=False)

现在,如果我以此为基础构建数据库,我会得到一个 table,类似于:

cities:
+----------+------------------------+---------------------------------------------------------+
| Column   | Type                   | Modifiers                                               |
|----------+------------------------+---------------------------------------------------------|
| id       | integer                |  not null default nextval('main_city_id_seq'::regclass) |
| name     | character varying(255) |  not null                                               |
| mayor_id | integer                |  not null                                               |
+----------+------------------------+---------------------------------------------------------+

capitals
+-------------+---------+-------------+
| Column      | Type    | Modifiers   |
|-------------+---------+-------------|
| city_ptr_id | integer |  not null   |
| has_embassy | boolean |  not null   |
+-------------+---------+-------------+

这不是主意,因为这意味着要获得首府城市的市长,我必须进行 2 次连接,一次从 capitalscities,然后从 citiesmayors.

在 Postgres 中,我们可以有:

cities:
+------------+-------------+------------------------------------------------------+
| Column     | Type        | Modifiers                                            |
|------------+-------------+------------------------------------------------------|
| id         | integer     |  not null default nextval('cities_id_seq'::regclass) |
| name       | text        |                                                      |
| mayor_id   | realinteger |                                                      |
+------------+-------------+------------------------------------------------------+

where the below table is listed as a 'child'

capitals:
+------------+--------------+------------------------------------------------------+
| Column     | Type         | Modifiers                                            |
|------------+--------------+------------------------------------------------------|
| id         | integer      |  not null default nextval('cities_id_seq'::regclass) |
| name       | text         |                                                      |
| mayor_id   | realinteger  |                                                      |
| embassy    | bool         |                                                      |
+------------+--------------+------------------------------------------------------+

有没有办法在 Django 中使用 Postgres' table inheritance

提前致谢

不幸的是,这在 Django 本身和任何第 3 方包中都没有实现。如果不对 Django 的核心进行一些重大更改,甚至可能无法创建第三方包。

如果您对此功能感兴趣,ticket 在 Django 的错误跟踪器中有关于此确切功能的文章。

请记住,这种类型的继承确实有一些主要的限制,比如指向父的外键 table 不能处理子实例,父子之间不共享索引(没有出-针对父项和所有子项 tables) 等

之间的唯一性的开箱即用解决方案