Valentino Langarosa
Valentino Langarosa

Reputation: 307

select some fields in eagerload

I have two tables:

class Auto(Base):

    __tablename__ = "auto"

    id = Column(Integer, primary_key=True)
    added = Column(DateTime())

and

class Url(Base):

    __tablename__ = "url"

    id = Column(Integer, primary_key=True)
    added = Column(DateTime())
    auto_id = Column(Integer, ForeignKey('auto.id'))
    url = Column(Unicode(500))
    content = Column(UnicodeText())

    auto = relation('Auto', backref=backref('url', order_by=id))

I selecting data with

Session.query(Auto).options(eagerload('url')).limit(20)

In select statement i get all fields relative to Auto and Url tables, but i want to select only some results (Auto.id, Auto.added, Url.id, Url.added). Problem is what in Url.content store large amount of data.

Upvotes: 2

Views: 320

Answers (1)

Denis Otkidach
Denis Otkidach

Reputation: 33200

Like you are using option eagerload() for relations, there is an option defer() for column properties to disable loading them untill they are accessed:

session.query(Auto).options(joinedload('url'), defer('url.content')).limit(20)

Upvotes: 0

Related Questions