[Answer]-How to get unique values in Django models?

1👍

1st

len(queryset) is a bad way to count on objects. Use .count() instead

bags_closed = Shipment.objects.filter(bags__status__exact='close', 
                                      bags__time__range=[start_time,end_time],
                                      pptls__pptl_id__exact = 147)\
                              .distinct('bags').count()

2nd

last_bagged_on = ShipmentBagSealMapping.objects.filter(
    status__exact='close',
    time__range=[start_time,end_time],
    shipment_id__pptls__pptl_id__exact=pptls.pptl_id)[0]

If you’re using Django 1.6, you should use last_bagged_on = ShipmentBagSealMapping…first() instead of [0]

Leave a comment