On 22.7.2006, at 18.10, Brett wrote:
> I find it's easy enough to just create new types from existing
> types to
> do the validation. e.g. I created a simple Enum type this way...
>
> class Enum(types.Unicode):
>
> def __init__(self, values, empty_to_none=False):
> '''
> contruct an Enum type
>
> values : a list of values that are valid for this column
> empty_to_none : treat the empty string '' as None
> '''
> if values is None or len(values) is 0:
> raise exceptions.AssertionError('Enum requires a list of
> values')
> self.empty_to_none = empty_to_none
> self.values = values
> # the length of the string/unicode column should be the
> longest
> string
> # in values
> super(Enum, self).__init__(len(max(values)))
>
>
> def convert_bind_param(self, value, engine):
> if self.empty_to_none and value is '':
> value = None
> if value not in self.values:
> raise exceptions.AssertionError('%s not in Enum.values' %
> value)
> return super(Enum, self).convert_bind_param(value, engine)
>
>
> def convert_result_value(self, value, engine):
> if value not in self.values:
> raise exceptions.AssertionError('%s not in Enum.values' %
> value)
> return super(Enum, self).convert_result_value(value, engine)
>
That really is easy! I think this solution works for me. Thanks Brett!
Is there a way to check these validations before trying to commit
changes to database by flush()?
Cheers,
Joona Kulmala
--
Joona Kulmala <[EMAIL PROTECTED]>
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users