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?
Here's the trimmed down version of my models:
class Shipment(Model):
user = ForeignKey(User, related_name='shipments')
class ShipmentItem(models.Model):
shipment = ForeignKey(Shipment, related_name='items')
class Meta:
abstract = True
class VehicleItem(ShipmentItem):
pass
class Bid(Model):
user = ForeignKey(User, related_name='bids')
shipment = ForeignKey(Shipment, related_name='bids')