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

  • Home
  • SEARCH
  • 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 5945761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:43:37+00:00 2026-05-22T16:43:37+00:00

I have the following code in my model, which determines if an Entry is

  • 0

I have the following code in my model, which determines if an Entry is billable:

class Entry(models.Model):
    [ .. ]
    @property
    def is_billable(self):
        return self.tags.filter(billable=False).count() == 0

Entry has an FK to Project. When determining the remaining budget on a Project, I loop through all the entries, and check if is_billable returns True:

@property
def remaining_budget(self):
    [ .. ]
    for entry in self.entry_set.all():
        if entry.is_billable:
            remaining_budget -= entry.minutes

Which is kind of heavy for the database, because it’ll fire a query for each Entry. I’m looking for a way to optimize this, tips and hints are welcome.

  • 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-22T16:43:38+00:00Added an answer on May 22, 2026 at 4:43 pm

    If you need to get rid of the count query then why not:

    1. Add an is_billable field to your Entry model
    2. Write signals based on the tag objects to set this field to True or False (if a new tag with is_billable == True is added, then set it True / if a tag is removed and no tags with is_billable == True remain, then set it False, etc)

    Of course if your data can be changed through other apps or manually, this method should not be preferred.

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

Sidebar

Related Questions

I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have a view which accepts the following model: Inherits=System.Web.Mvc.ViewPage<MVC_WordsByME.Models.JobCreationModel> This posts back to
I have the following code in my model: attr_accessor :expiry_date validates_presence_of :expiry_date, :on =>
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a
I have following code class User attr_accessor :name end u = User.new u.name =
I have a model similar to the following: class Foo attr_accessor :attribute_a # Really
Okay, I have the following code: QFileSystemModel *model = new QFileSystemModel; model->setRootPath(QDir::currentPath()); model->setFilter(QDir::Files |
I have following code to find if row is selected, which is selected and

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.