5

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?

Arif
  • 317
  • 2
  • 12

1 Answers1

2

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.

Anton Kraievyi
  • 4,182
  • 4
  • 26
  • 41