Home > Enterprise >  Django: how to sum up numbers in django
Django: how to sum up numbers in django

Time:04-28

i want to calculate the total sales and display the total price, i dont know how to write the function to do this. I have tried writing a little function to do it in models.py but it working as expected. This is what i want, i have a model named UserCourse which stored all the purchased courses, now i want to calculate and sum up all the price of a single course that was sold.

models.py

class Course(models.Model):
    course_title = models.CharField(max_length=10000)
    slug = models.SlugField(unique=True)
    price = models.IntegerField(default=0)

class UserCourse(models.Model):
    user = models.ForeignKey(User , null = False , on_delete=models.CASCADE)
    course = models.ForeignKey(Course , null = False , on_delete=models.CASCADE)
    date = models.DateTimeField(auto_now_add=True)

and also how do i filter this models in views.py so i can display the total price of courses a particular creator have sold.

CodePudding user response:

You can use the aggregate functionality in your view:

from django.db.models import Sum

price_total = UserCourse.objects.filter(
        course__title="a title"
    ).aggregate(
       aggregate_price = Sum("course__price")
    )["aggregate_price"]

To break it down:

First we get filter all the UserCourse objects where the course has a title of "a title"

Then we aggregate them, finding the sum of the course prices for all those courses picked up by the filter

By naming a variable in our Sum expression, the queryset will now have a named value that we can get by the name ['aggregate_price'] and assign to price_total

Now you can pass price_total via context to use in your template.

You can specify other filters to get price totals for different querysets. So if your creator is the user field in your UserCourse model, you can repeat the process with user__username = "my_username" as the filter

CodePudding user response:

Try this approach using aggregate where you filter all UserCourse objects from a particular course, then run an aggregate on the price for that course:

my_course = Course.objects.first()

total_amount_sold = UserCourse.objects.filter(
    course=my_course,
).aggregate(total_amount_sold=Sum("course__price"))["total_amount_sold"]

CodePudding user response:

class Course(models.Model):
    course_title = models.CharField(max_length=128)  # Don't make max_length too big
    slug = models.SlugField(unique=True)
    price = models.IntegerField(default=0)
    customer = models.ManyToManyField(User, through='UserCourse', on_delete=models.CASCADE)


class UserCourse(models.Model):
    user = models.ForeignKey(User, null=False, on_delete=models.CASCADE)
    course = models.ForeignKey(Course, null=False, on_delete=models.CASCADE)
    date = models.DateTimeField(auto_now_add=True)


# get courses by user filter
first_customer = User.objects.first()
first_customer_courses = first_customer.course_set.all()

# get sum of customer courses price
sum_price = sum([course.price for course in first_customer_courses])

if you want to render sum_price in view, you can set it in view.context.

  • Related