Re: [sqlalchemy] how to iterate over Enum's possible values

2,820 views
Skip to first unread message

Michael Bayer

unread,
Aug 1, 2012, 11:58:36 AM8/1/12
to sqlal...@googlegroups.com
The values are in there as "Calc.choose.property.columns[0].type_.enums".  But this isn't how you'd want to go about this, you want to make your particular enum here a Python type you can work with, something simple might be:

choose_type = set(['min', 'max', 'avg'])

class Calc(...):
   ...
   choose = Column(Enum(*choose_type))

that's a quick version.  for the real enum pattern that I use, see http://techspot.zzzeek.org/2011/01/14/the-enum-recipe/




On Aug 1, 2012, at 4:04 AM, jeetu wrote:

I have a table Calc. I have an Enum type "choose" in it. In some other part of the code, I wan to have the possible values of this Enum type. How I can achieve this?

# Present Calc class in Calc.py file
class Calc(DeclarativeBase):
     __tablename__= calcs
     id =  Column(Integer, primary_key=True)
     choose = Column(Enum('min', 'max', 'avg')

# Desired in some other file
import Calc
for choice in Calc.choose: print choices # I want something like this to iterate over options of 'choose' in 'Calc'.

PS: I am using turbogears 2.2rc2


--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/mF1ZWU6RFEAJ.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

jeetu

unread,
Aug 1, 2012, 1:41:14 PM8/1/12
to sqlal...@googlegroups.com
Thanks Michael,
I actually saw that page of yours before posting this question. That is awesome work, especially for the various ways in which it enables the developer. Ideally this whole functionality should be provided by the ORM as Enum is a fully supported type in sqlalchemy (I suppose). Presently my requirement is only to list/dropdown it for UI purpose. Though it looks complex, I'll go with "Calc.choose.property.columns[0].type_.enums". But i will put a remark in my source code to refer to your solution page whenever any further complex processing is required.
Once again thanks,.
To unsubscribe from this group, send email to sqlalchemy+unsubscribe@googlegroups.com.

jeetu

unread,
Aug 2, 2012, 12:12:48 AM8/2/12
to sqlal...@googlegroups.com
Just for other people's reference, the working statement is
     Calc.choose.property.columns[0].type.enums
Reply all
Reply to author
Forward
0 new messages