> > HI! > > I implemented a TypeDecorator which allows setting the precision of a > datetime value. > > It does something like: > > *import datetime* > * > * > *class PrecisionDateTime(TypeDecorator):* > * def process_bind_param(self, value, dialect):* > * return datetime.datetime(value.year, value.month,* > * value.hour, value.minute,* > * 0, 0)* > > > This works but the problem is, that I have to call Session.refresh() on > every added (and flushed) > object because otherwise the date-column still has the old value (with > second != 0 and millisecond != 0). > > Is there a way the refresh could be avoided? > > > you'd want to switch/augment by using a @validates rule: > > > http://docs.sqlalchemy.org/en/rel_0_8/orm/mapper_config.html?highlight=validates#simple-validators > > Thanks for the fast answer Michael!
Your suggestion brought me onto the right track! I solved it like this: *def create_dt_precision_validator(precision):* * def validate_precision_date_time(target, value, oldvalue, initiator):* * return change_date_precision(value, precision)* * return validate_precision_date_time* * * * * *@event.listens_for(orm.mapper, 'mapper_configured')* *def add_precision_dt_validators(mapper, class_):* * for prop in mapper.iterate_properties:* * if isinstance(prop, ColumnProperty):* * for column in prop.columns:* * if isinstance(column.type, PrecisionDateTime):* * event.listen(prop, 'set',* * create_dt_precision_validator(* * column.type.precision),* * retval=True)* Somehow I like it better when I am able to define such rules at the column level. But the @validators features is good to know! Thanks a lot, Florian -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.