Get sum of the salary for each user

I am on a django project where the admin assigns work to users by uploading the work to each user, and the user can only see the job they have been assigned. I want a way to get the total amount for all the work the user have done by the end of a month. I have a model Work, and it has a field Pay_Amount, which is the amount for one job, I want a way to add the Pay_Amount of only one user per month. I already have the total amount to pay all the users, I just want the amount to pay a single user for all the jobs he/she has done for one month... Please assist...

Try giving your code it will make easy for us to understand

If you have User object, that you get i.e. with that function:

user = User.objects.get(id=1)

Then you might have get all the total related work objects:

user.work_set.all()

To sum total Pay_amount:

from django.db.models import Sum
user.work_set.aggregate(Sum("Pay_amount")).get("Pay_amount__sum")

And to limit that to one month (let's say August this year):

user.work_set.filter(Date_Created__month=8, Date_Created__year=2022).aggregate(Sum("Pay_amount")).get("Pay_amount__sum")

PS name classes with CamelCase and fields/variables with snake_case. It is pretty important.

Back to Top