python 3.x - SQLAlchemy: Formatting a db.DateTime() column result -
i have relative simple sqlalchemy query (on mysql database):
my_date = db.session.query(func.max(mytable.date_column)).one()
the queried column of type db.datetime()
.
now want format returned date in my_date
:
my_date.isoformat() # fails my_date.strftime("%y-%m-%d %h:%m:%s %z") # fails
what object result , have datetime object can formatted?
when use debugger inspect returned object see following: (datetime.datetime(2016, 1, 28, 12, 35, 17),)
- real python datetime.datetime object looks different in debugger.
dir(my_date)
returns following:
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_asdict', '_fields', '_real_fields', 'count', 'index', 'keys']
one()
returns result keyedtuple.
you can use scalar()
my_date = db.session.query(func.max(mytable.date_column)).scalar()
or value tuple
my_date = db.session.query(func.max(mytable.date_column)).one()[0]
Comments
Post a Comment