【问题标题】:How to manage Postgres Enum type in Django?如何在 Django 中管理 Postgres 枚举类型?
【发布时间】:2021-08-21 11:08:48
【问题描述】:

我曾使用django-mysql 从 Django 管理 MySQL 枚举类型。最近我在我的 Django 项目中使用 Postgres 数据库。要管理Postgres Enum,我找不到任何库。 Django 中有一个db_type() 方法来管理自定义类型。但问题是必须先在数据库中创建 Postgres 枚举,然后才能使用它。

这是 Postgres 枚举的代码示例。

CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE person (
    name text,
    current_mood mood
);

有图书馆吗? 如果没有,我怎么能从 Django 实现 Postgres 枚举管理?

【问题讨论】:

    标签: django postgresql enums


    【解决方案1】:

    好吧,如果架构是预先烘焙的,那么 defining the same choices and pretending the field is text-valued 就可以了。刚刚用 Postgresql 13.3 和 Django 3.2.6 / psycopg2 2.9.1 测试了这个。

    否则我看到其他人使用自定义数据库架构管理变魔术 -- herehere

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      • 1970-01-01
      • 2014-11-06
      • 2018-12-30
      • 2016-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多