I was wondering about this "choices/enum" myself at one stage.

Having tried Django some time ago, I already had a set of such tuple constants, which I wanted to simply reuse in Elixir. I did think that an enum column type in Elixir would be very handy (like a tried in a version using SQLObject) to have though. One can probably pick it up from the lower SQLAlchemy somehow, but I have no idea how to tackle it at that level without going the whole SQLAlchemy route, which I didn't - I reallt like the look and feel of Elixir.

I now do my own "choices" lookup as shown in code snippets below. (Please forgive any wayward coding - I'm certainly no boffin, and am only offering this as an example of how I worked my (lame, perhaps) way around this problem.

In choices.py:

VERSION_STATUS_CHOICES = (
   (1, 'Draft'),       # Active
   (2, 'Approved'),    # Active
   (3, 'Ratified'),    # Active
)

# Finds index of given choice value in given choices tuple
def find_index(value, choices):
   """Finds index of given choice value in given choices tuple.

Returns integer (index)."""
   for x in choices:
       if x[1] == value:
           return x[0]
   return 0

# Finds value of given choice index in given choices tuple
def find_value(index, choices):
   """Finds value of given choice index in given choices tuple.

Returns string (value)."""
   for x in choices:
       if x[0] == index:
           return x[1]
   return 'None'

In model.py:

import choices
from elixir import *
from datetime import datetime

class VersionStatus(Entity):
   with_fields(
       status_id = Field(Integer, nullable=False, default=1),
       date = Field(DateTime, nullable=False, default=datetime.now)
   )
   belongs_to('version', of_kind='Version', required=True)
using_options(
       tablename='version_status',
       order_by=['date']
   )
   def get_status(self):
return choices.find_value(self.status_id, choices.VERSION_STATUS_CHOICES) def set_status(self, status): self.status_id = choices.find_index(status, choices.VERSION_STATUS_CHOICES) status = property(get_status, set_status) def __repr__(self): return "%s(date='%s', status=%s)" % (self.__class__.__name__, self.date, self.status_id)

   def __str__(self):
       return '%s' % (self.status)

I hope this helps your immediate (short-term?) problem. I would also like to see an easy way to have this sort of functionality at the Elixir level.

Jacques

Jonas wrote:
An option interesting to add in SQLElixir would as 'choices' of Django
[1].

An iterable (e.g., a list or tuple) of 2-tuples to use as choices for
a field.
The first element in each tuple is the actual value to be stored. The
second element is the human-readable name for the option, and that
could be used in the admin interface created by the user).

It could be created from type enum [2].

[1] http://www.djangoproject.com/documentation/model-api/#choices
[2] http://www.sqlalchemy.org/trac/wiki/UsageRecipes/Enum


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SQLElixir" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---


Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to