I have a question On Django annotations. Here is my model:
class Asset(models.Model):
security = models.ForeignKey(Security, on_delete=models.CASCADE, blank=False)
class Batch(models.Model):
fk_asset = models.ForeignKey(Asset, on_delete=models.CASCADE, blank=False)
class BatchPosition(models.Model):
fk_batch = models.ForeignKey(Batch, on_delete=models.CASCADE)
quantity = models.DecimalField(max_digits=14, decimal_places=2, blank=False)
In my view I would like to annotate the total Sum of Batchposition->quantity to the Asset
I tried things like
Asset.objects.annotate(Sum("batch")).annotate(Sum("batchposition__quantity"))
…but can’t get it to work. What am I doing wrong here? Is it finally possible to achieve this?