you're close, I've added a note to the "validates" documentation at 
http://www.sqlalchemy.org/docs/orm/mapper_config.html#simple-validators that 
will point a reader on to the more comprehensive solution.

I've added an example for you as well as Jason who asked almost the same 
question earlier, which illustrates the TypeDecorator in conjunction with an 
attribute listener that is applied to all occurrences of the target type, an 
approach also used by the mutable attributes extension, and which we may look 
into adding as more of a "built in" feature in the future although the recipe 
should be straightforward.

http://www.sqlalchemy.org/trac/wiki/UsageRecipes/ValidateAllOccurrencesOfType


On Jun 30, 2011, at 2:42 AM, Russ wrote:

> I discovered the sqlalchemy.orm.validates decorator, which executes
> when I want it to (on attribute assignment) so I got out my hacksaw
> and tried to make it work in one fell swoop... implementing automatic
> UTC assignment for all UTCEnforcedDateTime columns.
> 
> I'm not comfortable with it yet... but a mostly-inclusive demo code
> snippet is here:
> http://pastebin.com/wB4BLzax
> 
> Where I intend to do that last bit of hackery (utcConversionList and
> __sa_validators__ work) by introspecting through a large number of
> classes (all having the TableDefMixin) and looking for
> UTCEnforcedDateTime Column definitions that should be added to the
> utcConversionList for the class.  Method tbd there, but not important.
> 
> Although it is doing what I want it to do ("hands off" UTC assignment
> where expected), I'm really not comfortable with it.  It seems
> *highly* sketchy, quite indirect,  and it seems like there must be a
> better way to simply set a default validator for a custom Column
> type.  Is there?
> 
> It would be much tidier to put the validator into the
> UTCEnforcedDateTime class.  If not... can I rely on the direct setting
> of __sa_validators__ working in the future?  Using the orm.validates
> decorator in each and every class is obviously the better choice for
> future compatibility, but if I want one-shot assignment as I do above,
> I can't use the orm.validates because it is too late to set
> __sa_validators__ directly as orm.validates does, and I needed to be
> brutal and go to the __dict__ directly.
> 
> I expect there is a simple answer and all my hacking/exploration
> related to my posts above was pointless... except it has been highly
> educational to root through the SQLAlchemy implementation here.
> Tidbits learned include now understanding descriptors (never had cause
> to before), and learning about attrgetter and attrsetter.  Stepping
> into the attribute assignment (InstrumentedAttribute.__set__) was
> highly confusing until reading up on those bits!!  instance_state()
> and instance_dict() instantly returning was somewhat mysterious for a
> while!
> 
> Thanks,
> Russ
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

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

Reply via email to