I am currently working with three Django models and need assistance in improving the efficiency of my queries.
class ShipmentPPTLMapping(models.Model):
pptl_id = models.CharField(max_length=255)
shipment_id = models.ForeignKey('Shipment', related_name='pptls')
class ShipmentBagSealMapping(models.Model):
bag_seal = models.CharField(max_length=255)
status = models.CharField(max_length=255, default='open')
shipment_id = models.ForeignKey('Shipment', related_name='bags')
class Shipment(models.Model):
job_id = models.CharField(max_length=255)
I need to retrieve all bags from ShipmentBagSealMapping that have a status of ‘closed’ based on a specific pptl_id from ShipmentPPTLMapping.
Currently, I am using multiple queries and inner loops:
shipment_pptl_mapping_list = ShipmentPPTLMapping.objects.filter(pptl_id=pptl_id)
for shipment_mapping in shipment_pptl_mapping_list:
closed_bags = shipment_mapping.shipment_id.bags.filter(status='closed')
for bag in closed_bags:
# process each bag
pass
This results in numerous database queries, and I am looking to streamline this to a single query. Is it possible to get all the closed bags directly without needing to loop through the ShipmentPPTLMapping items first?
I am aiming for something resembling:
closed_bags = OptimizedQuery
for bag in closed_bags:
# process bag
pass
What is the most effective way to merge these relationships into one optimized Django query?