2012-07-04 3 views
2

는 좀 SQLAlchemy의 쿼리가 있습니다대신 'query.add_column'대신 엔티티에 필드를 추가 하시겠습니까?

>>> items = (
    Company.query 
    .add_column(Address.city) 
    .join(Company.address) 
    .filter(and_(
     ...some filters... 
    )) 
    .all() 
) 
>>> items 
[(Company(6239), Berlin), (Company(5388), Moscow), ...] 

어떻게 엔티티 회사에 Address.city를 넣어, 내 쿼리를 수정할 수 있습니까? 다음과 같이보고 싶습니다.

... 
>>> items 
[Company(6239), Company(5388), ...] 
>>> items[0].city 
Berlin 

고마워요.

답변

3

모델을 수정하지 않고도 쿼리에서 직접 수행 할 수 있다고 생각하지 않습니다.
그러나 당신은 쉽게 코드에서이 achive 수 : I 시도와 노력

>>> items 
[(Company(6239), Berlin), (Company(5388), Moscow), ...] 

>>> # set company 
>>> for (_company, _city) in items: 
     _company.city = _city 
>>> # remove city attribute 
>>>> items = [_company for (_company, _city) in items] 

>>> items 
[Company(6239), Company(5388), ...] 
>>> items[0].city 
Berlin 
+0

하지만,이 모든이었다 내가 :(뿐만 아니라 함께 올 수 – Josh

관련 문제