我想要一个包含整个OrderInfo对象的列表,例如,如果执行此操作,则它是我想要的结果。

def find_all(self):
    result_list = []
    orderDao = DaoUtil.DaoGeneric()
    session = orderDao.getSession()
    try:
        for row in session.query(OrderInfo).all():
            result_list.append({
                'id':row.id,
                'name': row.name,
                'age': row.age,
                 'create_time': row.create_time.strftime("%Y-%m-%d %H:%M:%S"),
                 'update_time': row.update_time.strftime("%Y-%m-%d %H:%M:%S"),
                    'version': row.version
                })
            session.commit()
    except Exception, e:
         print e
         session.rollback()
    return  result_list


但我想要一个包含查询中的OrderInfo对象的列表,因为查询返回的结果除OrderInfo {id,name,age,create_time,update_time,version}外还有其他列(所有DeclarativeBase实例的简单列表),查询不会直接返回OrderInfo对象。我要的以下内容:

def find_all(self):
    result_list = []
    orderDao = DaoUtil.DaoGeneric()
    session = orderDao.getSession()
        try:
            for row in session.query(OrderInfo).all():
                result_list.append(row.orderInfo) // if the row has a property for orderInfo Object, because the result which java can achieve , the example  for java is :   List<OrderInfo> orderList = session.query(); please help to achieve it


            session.commit()
    except Exception, e:
        print e
        session.rollback()
    return  result_list


因为我刚才在python中使用sqlalchemy,所以我不太确定。如何从SQLAlchemy中的查询获取包含OrderInfo对象的列表

最佳答案

@univerio

for row in session.query(OrderInfo).all():


变量行的行,其中包括以下列:

_decl_class_registry,
_sa_class_manager,
_sa_instance_state,
metadata,
query,
id,
name,
age,
create_time,
update_time ,
version


仅此(OrderInfo {id,名称,年龄,create_time,update_time,version})是我想要的,其他我不想获取的列。

OrderInfo:

from sqlalchemy import Column, Integer, String, Date, DateTime
from sqlalchemy.ext.declarative import declarative_base
    Base=declarative_base()

    class OrderInfo(Base):
        __tablename__ =  'order_info'
        # __table__ =  'order_info'

        id = Column(Integer, primary_key=True)
        name = Column(String(100))
        age = Column(Integer)
        create_time = Column(Date)
        update_time = Column(Date)
        version = Column(Integer)

09-26 07:09