question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enum field support

See original GitHub issue

I’ve had a look around but can’t seem to find any reference to Peewee supporting enum fields, other than a short mailing list thread with no outcome, and a user contributed recipe for Postgres which is untested. There are also arguments for not using enum.

All the supported Peewee databases have support for enum fields, and a quick look at the source indicates it would be trivial to add it in.

Is there any reason why enum fields have not been added yet? Would you consider accepting a PR for this?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

14reactions
brataocommented, Feb 19, 2018

This code works good for Python 3.4+ enum for MySQL

class enum_field(CharField):
    """
    This class enable a Enum like field for Peewee
    """
    def __init__(self, choices, *args, **kwargs):
        self.choices = choices
        super(CharField, self).__init__(*args, **kwargs)

    def db_value(self, value):
        return value.name

    def python_value(self, value):
        return self.choices(value)

class my_table(Model):
    class StatusChoices(Enum):
        WAITING = 1
        READY_TO_SEND = 2
        UPLOADING = 3
        OCR = 4
        DONE = 5

    status = enum_like_field(null=False, choices=StatusChoices)

9reactions
leinardicommented, Mar 17, 2019

Yet another version of the EnumField. This version stores the enum value as CharField but restores the correct Enum type when reading it:

class EnumField(CharField):
    """
    This class enable an Enum like field for Peewee
    """

    def __init__(self, choices: Callable, *args: Any, **kwargs: Any) -> None:
        super(CharField, self).__init__(*args, **kwargs)
        self.choices = choices
        self.max_length = 255

    def db_value(self, value: Any) -> Any:
        return value.value

    def python_value(self, value: Any) -> Any:
        return self.choices(type(list(self.choices)[0].value)(value))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Support for EDM Enum Type
The EDM enumerated type is supported by the Windchill Rest Services framework. Declaring an EDM Enumerated Type. An enum type is defined in...
Read more >
enum — Support for enumerations — Python 3.11.1 ...
EnumType. The type for Enum and its subclasses. · Enum. Base class for creating enumerated constants. · IntEnum. Base class for creating enumerated...
Read more >
Documentation: 15: 8.7. Enumerated Types - PostgreSQL
Enumerated (enum) types are data types that comprise a static, ordered set of values. They are equivalent to the enum types supported in...
Read more >
How to support Enum value in Gorm - Stack Overflow
Good day guys, I am new to go and Gorm, I am building a simple web service using Postgres, I had an issue...
Read more >
ENUM - MariaDB Knowledge Base
Enumeration, or string object that can have one value chosen from a list of values.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found