Sum with filter on related queryset

I have the following models:

class GamificationContext(models.Model):
    # ... fields
 
    def get_users_with_annotated_reputation_total(self):
        return User.objects.all().annotate(
            reputation_total=Sum("reputation_deltas__delta", default=0) # <-- problem here
        )

class GamificationReputationDelta(TimestampableModel):
    context = models.ForeignKey(
        GamificationContext,
        related_name="reputation_deltas",
        on_delete=models.CASCADE,
    )
    user = models.ForeignKey(
        User,
        related_name="reputation_deltas",
        on_delete=models.CASCADE,
    )
    delta = models.IntegerField()

My issue is that the method get_users_with_annotated_reputation_total sums over all the reputation_deltas of users for all GamificationContexts, whereas I’d like to only get those that reference the object I’m calling the method on.

I have taken a look at the API of Aggregate and I couldn’t find an easy way to apply a restriction on the related objects I’m aggregating over. How do I achieve what I’m trying to accomplish here?

See Filtering on annotations

1 Like