I have a model created using Django ORM -
class Orders:
units = models.IntegerField(null=False, blank=False)
sell_price_per_unit = models.FloatField(null=True, blank=True)
created_by = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False)
order_status = models.ForeignKey(OrderStatus, on_delete=models.PROTECT, null=False, blank=False)
property = models.ForeignKey(Property, on_delete=models.PROTECT, null=False, blank=False)
order_type = models.ForeignKey(Property, on_delete=models.PROTECT, null=False, blank=False)
...
...
I want to write a query to select_for_update()
and the rows should be selected until the Sum('sell_price_per_unit')
reaches a given number. Additionally I have to apply certain filter and exclude certain rows based on the condition. filter()
& exclude()
were pretty straight forward so I already took care of it but I have no idea of how to select rows until the Sum('sell_price_per_unit')
reaches to a given number.
queryset = (
Orders.objects.select_for_update()
.exclude(created_by=created_by)
.values(
"id",
"property",
"units",
"created_by",
"sell_price_per_unit",
"order_status",
)
.filter(
property=order_obj.property,
order_type__name=SELL,
order_status__in=[PARTIALLY_COMPLETED[1], OPEN[1]],
sell_price_per_unit__lte=order_price,
)
.order_by("sell_price_per_unit", "created_at")
)
Is it possible to accomplish this?