Is there a way to simplify this Django query?

Posted by Mark on Stack Overflow See other posts from Stack Overflow or by Mark
Published on 2010-05-21T00:20:13Z Indexed on 2010/05/21 0:30 UTC
Read the original article Hit count: 227

Filed under:
|
|
accepted_bids = Bid.objects.filter(shipment__user=u, status='acc').select_related('shipment')
completed_shipments = []
for b in accepted_bids:
    completed_shipments.append(b.shipment)
vehicles_shipped = []
for s in completed_shipments:
    vehicles_shipped.extend(s.items.all())

In the end, I want a list of shipped vehicles. A vehicle is shipped if it's part of a shipment that's completed. A shipment is completed if it has an accepted bid.

I'd prefer not to iterate over the querysets thereby forcing a hit to the DB before its necessary... isn't there a way to get all the associated shipments from a list of bids, for example?

© Stack Overflow or respective owner

Related posts about django

Related posts about django-models