How can I create a model of a table with an enum in peewee 2?
Asked Answered
M

2

6

I'm trying to create a model to describe a table with an enum field in peewee.

I'm seeing that EnumField was removed from the peewee.py file before the 2.0 version, and I can't find anything in the current docs that outlines how to implement it. Does anyone know if I can just use CharField?

Mooneyham answered 17/12, 2012 at 22:35 Comment(3)
Did you ever implement an ENUM field using a custom peewee field? If so, would you mind sharing that solution with us?Pappose
Just use the choices Field attribute?Naoma
The choices field attribute didn't exist in 2012.Mooneyham
F
4

there was not an EnumField prior to 2.0, but there are docs on implementing a custom field: http://peewee.readthedocs.org/en/latest/peewee/models.html#creating-a-custom-field

I hope this helps.

Furfuraceous answered 5/1, 2013 at 1:16 Comment(1)
coleifer should that be part of standard peewee fields? I see choices option but if it doesn't validate value. I remember sqlobject used to support such field.Molecular
C
5

Here is a working solution for PostgreSQL:

https://gist.github.com/b1naryth1ef/607e92dc8c1748a06b5d

Clackmannan answered 20/11, 2014 at 17:7 Comment(2)
Why the downvote? With some feedback, I could maybe improve my answer...Clackmannan
hi I suggest pasting the code here as well in case the gist gets removed or the links becomes broken!Oberg
F
4

there was not an EnumField prior to 2.0, but there are docs on implementing a custom field: http://peewee.readthedocs.org/en/latest/peewee/models.html#creating-a-custom-field

I hope this helps.

Furfuraceous answered 5/1, 2013 at 1:16 Comment(1)
coleifer should that be part of standard peewee fields? I see choices option but if it doesn't validate value. I remember sqlobject used to support such field.Molecular

© 2022 - 2024 — McMap. All rights reserved.