On Aug 19, 10:40 am, "Heston James - Cold Beans"
<[EMAIL PROTECTED]> wrote:
> ... ZSI requires that datetime's in the objects be in
> Python Time Tuples as documented in the 'time' module.
>
> Is there any way in which I can configure SQLAlchemy to return me time
> tuples instead? Or perhaps a cheeky way I can convert from datetime.datetime
> to time.time?
Here's one way you can have a column look like a time tuple instead of
datetime.datetime:
from sqlalchemy import *
from sqlalchemy.orm import *
engine = create_engine("sqlite://", echo=True)
metadata = MetaData(bind=engine)
sm = sessionmaker(autoflush=True, bind=engine)
session = scoped_session(sm)
mapper = session.mapper
tabl = Table('testa', metadata,
Column('pk',Integer, primary_key=True),
Column('when',DateTime, default=func.now()) )
class Tabl(object):
when = property(lambda self: self.when2.timetuple())
#replaces original 'when' which is renamed below.
#see http://docs.python.org/lib/datetime-datetime.html
mapper(Tabl, tabl, properties=dict(when2=tabl.c.when))
metadata.create_all()
g10 = Tabl(pk=10) #when=now
session.commit()
q = session.query( Tabl )
for obj in q:
print "#%d: %s" % (obj.pk, obj.when)
#10: (2008, 8, 21, 1, 48, 39, 3, 234, -1)
print `obj.when2` #show datetime format
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" 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/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---