How to instead of 'query.add_column' add field to entity?

sqlalchemy

Solution

I do not think you can do it directly in a query without modifying the model. But you can easily achive this in the code:

>>> 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

Problem

I have some sqlalchemy query: ``` >>> items = ( Company.query .add_column(Address.city) .join(Company.address) .filter(and_( ...some filters... )) .all() ) >>> items [(Company(6239), Berlin), (Company(5388), Moscow), ...] ``` How can I modify my query, to put Address.city to entity Company? I want it to look as: ``` ... >>> items [Company(6239), Company(5388), ...] >>> items[0].city Berlin ``` Thank you.

Original source