简体   繁体   中英

How to manage Postgres Enum type in Django?

I had used django-mysql to manage MySQL enum type from Django. Recently I am using the Postgres database for my Django project. To manage the Postgres Enum , I could not able to find any library. There is a db_type() method in Django to manage custom type. But the problem is Postgres enum must be created first at database and after that, I can use it.

Here is a code sample for Postgres enum.

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

Is there present a library at all? If not, how could I be able to achieve the Postgres enum management from Django?

Well, if the schema is pre-baked then just defining the same choices and pretending the field is text-valued does work. Have tested this just now with Postgresql 13.3 and Django 3.2.6 / psycopg2 2.9.1.

Otherwise I've seen other people doing magic with custom database schema management --here and here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM