In my flask sqlalchemy based app, I have a model like this:
class Foo(object):
start_date = db.Column(db.DateTime())
It works fine but when I use the template engine to print out the date onto HTML page by doing {{ foo.start_date }}
, it'll print a ugly line like 2015-8-8 00:00:00
. Trying to come up with my own format, I thought of something clever (at least before I hit the problem)
class Foo(object):
start_date = db.Column(db.DateTime())
def __getattr__(self, name):
return "{0}-{1}-{2}".format(self.start_date.year....)
However, something internal of SQLAlchemy doesn't like this. When a new object is created, it'll raise the following exception:
File "<string>", line 6, in __init__
File "/home/wenliang/work/template/flask/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 526, in _declarative_constructor
setattr(self, k, kwargs[k])
File "/home/wenliang/work/template/flask/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 226, in __set__
instance_dict(instance), value, None)
File "/home/wenliang/work/template/flask/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 694, in set
state._modified_event(dict_, self, old)
AttributeError: 'NoneType' object has no attribute '_modified_event'
Any one seen this before?