Does prefetch or select_related help when using the related field in the model @property

Viewed 52

I have a model like this:

class Order(Model):
    ...
    @property
    def all_shipments_shipped(self):
        return all([shipment.is_shipped for shipment in self.shipments.all()])

class Shipment(Model):
    ...
    order = ForeignKey(Order, related_name='shipments', on_delete=SET_NULL, null=True)

    @property
    def is_shipped(self):
        return (calculate if shipped based on tracking status)

and a viewset like this:

@action(methods=['GET'], detail=False, url_path='num_not_shipped')
    def num_not_shipped(self, request):
        qs = self.request.user.orders.all()
        num_orders_not_shipped = len([i for i in qs if not i.all_shipments_shipped])
        return Response(num_orders_not_shipped, status=status.HTTP_200_OK)

Would adding

qs = qs.select_related('shipments')

before num_orders_not_shipped =..., in the viewset, help being as that the shipment is used in the model property and not in the viewset itself?

Thanks for the help

0 Answers
Related