Skip to content

Commit 78e2211

Browse files
committed
a solution that doesn't need a raw query
1 parent b268531 commit 78e2211

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

‎orm.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,21 @@
2020
Column('qty', Integer),
2121
)
2222

23-
mapper(Order, order)
23+
24+
class _DummyOrderLine:
25+
pass
26+
27+
28+
mapper(_DummyOrderLine, order_lines)
29+
mapper(Order, order, properties={
30+
'__lines': relationship(_DummyOrderLine)
31+
})
2432

2533

2634
from sqlalchemy import event
2735

36+
# standard decorator style
2837
@event.listens_for(Order, 'load')
2938
def custom_load(target, context):
30-
raw_lines = context.session.query(order_lines).join(order).filter_by(id=target.id).all()
31-
target._lines = {sku: qty for _, sku, qty in raw_lines}
39+
breakpoint()
40+
target._lines = {l.sku: l.qty for l in target.__lines}

0 commit comments

Comments
 (0)