How to aggregate an amount field in one model using another model in django

i have three models : User, Campaign and Donation. Donation model has donation amount, donated by each user against each campaign.

Campaign model

class Campaign(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    campaign_title = models.CharField(max_length=200, blank=True)

Donation model

class Donation(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    # this is many to one relationship, on_deleting user, user's donation details will be set to NULL
    campaign = models.ForeignKey(Campaign, on_delete=models.DO_NOTHING)
    donation_amount = models.IntegerField()

views.py file

def landing_page(request):
    # campaigns = Campaign.objects.all().order_by('-id')
    campaigns = Campaign.objects.all().order_by('-id')

    ////DO SOMETHING HERE TO SHOW TOTAL DONATION AGAINST EACH CAMPAIGN////

    return render(request, 'core/landing_page.html',{'campaigns':campaigns})

Using the current views.py file, I’m able to display all the campaigns, how do i pass the total donation against each campaign to the html file?

You need to use the ORM to perform a GROUP BY … SUM query. Something like this:

donation_totals = (
    Portfolio.objects
    .values("campaign").annotate(donation_total=Sum("donation_amount"))
)

This part of the docs will be helpful to learn more.

Always double-check if your models define default ordering inside its Meta class since that will probably mess with many of your aggregations.

Good luck.