Python 3.6 and sqlalchemy 1.2.10
result=db_query(db_session.query(orm_model).filter(orm_model.id==1,orm_model.time.between(1,2)).all())
if I return this result directly in a restful api:
return [x.to_json() for x in result]
Front end got empty content, and I found x.__dict__ has only one key "_sa_instance_state".
The "to_json()" is orm_model's function:
def to_json(self):
jsonobj = {}
for x in self.__dict__:
jsonobj[x] = self.__dict__[x]
if "_sa_instance_state" in jsonobj.keys():
del jsonobj['_sa_instance_state']
return jsonobj
If I print it before return, every thing is ok, front end got the right content.
print(result)
return [x.to_json() for x in result]
And if I remove the "all()":
result=db_query(db_session.query(orm_model).filter(orm_model.id==1,orm_model.time.between(1,2)))
every thing is ok, without print before return, front end got the right content.
The Model is:
Base = declarative_base()
class orm_model(Base):
__tablename__ = 'orm_model'
id = Column(INTEGER(11), primary_key=True)
user = Column(INTEGER(11))
...
def to_json(self):
...
def __repr__(self):
return '<orm_model %s>' % self.id
and I define the db_query is always with commit and rollback:
def db_query(sql=None):
try:
db_session.commit()
except:
db_session.rollback()
return False
else:
return sql
My English is poor, sorry.