我试图从我的问题表中检索问题,但我收到错误,问题类对象无法迭代。给出此错误的代码示例是
def get(self):
for quser in session.query(questions).all():
return jsonify(quser)
我需要在这做什么。我对此进行了大量搜索,但无法解决。请帮忙。
答案 0 :(得分:0)
如果我正确理解你想要什么......
您可以为您的班级实施此方法:
def to_dict(self):
if getattr(self, '_sa_free', None):
return {k: v for k, v in self.__dict__.iteritems()}
attr_names = getattr(self.__class__, '__attr_names', None)
if not attr_names:
attr_names = [prop.key for prop in
class_mapper(self.__class__).iterate_properties if
isinstance(prop, ColumnProperty) and not prop.key.startswith('_')]
for attr in dir(self.__class__):
if isinstance(getattr(self.__class__, attr), (InstrumentedAttribute, property)) \
and not attr.startswith('_'):
attr_names.append(attr)
setattr(self.__class__, '__attr_names', list(set(attr_names)))
return {attr: getattr(self, attr) for attr in attr_names}
然后:
return jsonify(**quser.to_dict())