Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6172699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:26:20+00:00 2026-05-23T23:26:20+00:00

I have the following Models set up in my app: Lesson Student Evaluation The

  • 0

I have the following Models set up in my app:

  • Lesson
  • Student
  • Evaluation

The Lesson and Student models have a many-to-many relationships through Evaluation. In the Evaluation model, I also store the fee received for the lesson. I know I should really store this in the Lesson model and then multiply by the number of students but sometimes for whatever reason a student may not pay the full amount for a lesson, so I need to keep track of it on an individual basis.

I’m trying to create a list of all lessons in a given time period and include a column which displays

  1. The total amount received for the lesson
  2. The total amount received for all lessons in that time period.

At the moment, I am just using a generic view to return all lessons and I have a simple method defined in my Lesson model which calculates the total fee received:

def total_fee(self):
    evaluations = self.evaluation_set.all()
    total = 0;
    for e in evaluations:
        total += e.fee_paid

    return total

However, I’m running the debug toolbar and this shows me that this view required 87 separate SQL queries when I ran it!

Obviously, I can’t do that every time. How can I fetch all of the data in one go?

Any advice would be greatly appreciated.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:26:20+00:00Added an answer on May 23, 2026 at 11:26 pm

    I think I would personally tackle this one using Django’s Aggregation functionality. It’s hard for me to conceptualize your database structure without seeing your models, but I think the call would look something like this:

    from django.db.models import Sum
    total = self.evaluation_set.all().aggregate(total=Sum('fee_paid'))['total']
    

    That should give you the sum of all the fees paid for everything in the evaluation set in 1 DB call, I think. Take that with a grain of salt as I haven’t tested it.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following 2 models in my Google App Engine datastore: class Search(db.Model):
in a django-tastypie app I have the following Django-models: class Car(models.Model): name=models.CharField('name',max_length=64) class CarTrack(models.Model):
I have the following models set up: class Contact < ActiveRecord::Base belongs_to :band belongs_to
I have following models setup in my Django application class School(models.Model): name = models.TextField()
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()
I have the following models: class ProjectUser(models.Model): categories = models.ManyToManyField('UserCategory', blank=True, null=True) user_id =
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have the following structure: class FeatureType(models.Model): type = models.CharField(max_length=20) def __unicode__(self): return self.type
I have the following java model class in App Engine: public class Xyz ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.