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 9094611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:23:23+00:00 2026-06-16T23:23:23+00:00

It seems that Django developers are aware about the fact that there’s no way

  • 0

It seems that Django developers are aware about the fact that there’s no way to filter on calculated fields like this in MySQL:

SomeModel.objects.extra(select={ 'total': 'SOME CALCULATION GOES HERE' }, where=['total < 10'])

OR

SomeModel.objects.extra(select={ 'total': 'SOME CALCULATION GOES HERE' }).filter(total__lt=10)

This was described in https://code.djangoproject.com/ticket/8784 but unfortunately it looks like it will take another year to implement with current speed. I understand the concern of development team that ideally they want to get rid of the .extra() because by writing pure SQL in that statement code becomes non DB-agnostic. I believe that dealing with calculated field is fairly common task in many projects and I currently faced the problem that I can’t solve: I think the majority of community often uses this dirty workaround:

SomeModel.objects.extra(select={ 'total': 'SOME CALCULATION GOES HERE' }, where=['1 HAVING total < 10'])

and in fact it works, but only till the moment when you need to add filter on the result of the statement above:

SomeModel.objects.extra(select={ 'total': 'SOME CALCULATION GOES HERE' }, where=['1 HAVING total < 10']).filter(id__gt=300)

this doesn’t work anymore, because it translates to the wrong SQL statement:

SELECT id, ..., (SOME CALCULATION) AS total
FROM some_table
WHERE (1 AND HAVING total < 10 AND id > 300)

What I want to achieve is to have some kind of restriction in custom model manager which would allow me to write code like this:

class SomeModelManager(models.Manager):
  def apply_total_restriction(self, restriction_num):
    return self.extra(select={ 'total': 'CALCULATION GOES HERE' }).filter(total__lt=restriction_num)

...

SomeModel.objects.apply_total_restriction(restriction_num).filter(created_by_id__exact=1)

Question is: does anybody know how to overcome this temporary (hopefully) restriction and to fulfill the task? Any possible alternatives?

  • 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-06-16T23:23:24+00:00Added an answer on June 16, 2026 at 11:23 pm

    I’ve found the solution: I thought it will be too slow to calculate that field again in WHERE clause, but when I’ve tried it it turned to be quite fast and no HAVING required. Below is the demonstration of what I meant:

    SELECT (a + b / 2) as 'c', a, b
    FROM table
    WHERE (a + b / 2) >= 20 AND a != b

    That’s basically it – if you think you need HAVING, try to measure time with HAVING and compare it to time with calculating calculated field again and filter by it in WHERE and then make a conclusion.

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

Sidebar

Related Questions

It seems that Django's object model filter method automatically uses the AND SQL keyword.
I have a Django Model that implements a time range, like this: class Period(models.Model):
I'd like to go into Django with Python, but it seems that some hosting
I'm kind of confused by this because it seems that Django templates have optional
That seems simple enough, but all Django Queries seems to be 'SELECT *' How
Basically, I currently have login/ in urls.py redirect to the django.contrib.auth.views.login and that seems
Seems that This will be an easy question for you but this problem is
It seems that there is a implementation of rope in my /usr/include/c++/4.5.1/ext/rope (and ropeimpl.h
I ask this because there seems to be a few more jobs available (at
I see this question but it's from 2008. Seems like many version control systems

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.