OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python Django Annotations over multiple relationships

  • Thread starter Thread starter Dennis
  • Start date Start date
D

Dennis

Guest
I have a question On Django annotations. Here is my model:

Code:
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

Code:
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?
<p>I have a question On Django annotations. Here is my model:</p>
<pre><code>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)
</code></pre>
<p>In my view I would like to annotate the total Sum of Batchposition->quantity to the Asset</p>
<p>I tried things like</p>
<pre><code>Asset.objects.annotate(Sum("batch")).annotate(Sum("batchposition__quantity"))
</code></pre>
<p>...but can't get it to work. What am I doing wrong here? Is it finally possible to achieve this?</p>
 

Latest posts

Top