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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T08:20:31+00:00 2026-05-17T08:20:31+00:00

I have the following django model (mapped to table ‘A’): class A(models.Model): name =

  • 0

I have the following django model (mapped to table ‘A’):

class A(models.Model):
    name = models.CharField(max_length=64, null=False)
    value = models.IntegerField()
    ...

I want to perform the following simple query on top:

select avg(case 
        when (value > 0 and value <= 50) then 0 
        when (value > 50 and value < 70) then 50 
        else 100 end) 
from A
where ...

I’m trying to avoid raw SQL – How can this be implemented with django (in the above example I’m using avg, but the same question is also relevant for max, min, sum etc.)?

I tried using extra and aggregate:

extra(select={'avg_field': case_when_query})

and

aggregate(Avg('avg_field')), 

but the aggregate function only works with model fields so the extra field cannot be used here.
How can this be done with django?

Thanks for the help

  • 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-17T08:20:31+00:00Added an answer on May 17, 2026 at 8:20 am

    What can be done that will still allow us to use django queryset is something like this:

    qs = A.objects.extra(select={"avg_field": 
                         "avg(case when...)"}).filter(...).values("avg_field")
    

    To use the result:

    qs[0]["avg_field"]
    

    And this would allow the needed functionality.

    • 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 the following Django models: - class Company(models.Model): name = models.CharField(max_length=50) is_active =
I have the following Django 1.2 models: class Category(models.Model): name = models.CharField(max_length=255) class Article(models.Model):
I have the following Django model: class Make: name = models.CharField(max_length=200) class MakeContent: make
In my Django App I have the following model: class SuperCategory(models.Model): name = models.CharField(max_length=100,)
I have the following model structure below: class Master(models.Model): name = models.CharField(max_length=50) mounting_height =
I have the following multi-table inheritance situation: from django.db import Models class Partner(models.Model): #
I have the following django model: RESOURCE_DIR = os.path.join(settings.MEDIA_ROOT, 'resources') class Resource(models.Model): title =
I have the following model in Django: class Bout (models.Model): fighter_1 = models.ForeignKey(Fighter, related_name=bout_fighter_1)
Lets say, I have following models in my Django app. class EventGroup name =

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.