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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:07:09+00:00 2026-05-27T04:07:09+00:00

I have a table with columns, a, b, c, d, e. Each column is

  • 0

I have a table with columns, a, b, c, d, e.

Each column is given a random numerical value.

I can then order by that numerical value.

query_results = UserProfile.objects.all().order_by('a')

What i want is the ability to order by the sum of 2 or 3 or even 5 columns. But i cant figure out how to do it lightly.

My answer so far has been some what convoluted. I add an extra column x.

cursor.execute("UPDATE mysite_userprofile SET x = a+b;")

I then order by x. I have to update the column x each time i want different columns added together.

This is not the way to do it i know. i know order_by(‘a+b’) doesn’t work but is their another solution just as simple. I looked on django’s site but couldn’t find anything

  • 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-27T04:07:10+00:00Added an answer on May 27, 2026 at 4:07 am

    The best way is to use extra

    UserProfile.objects.extra(
        select={'sum_of_fields': 'a + b + c + d + e'},
        order_by=('sum_of_fields',)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that has three different date columns, so I set each
I have a table that contains three user ID columns and an Active column
I have a table column where each row has one of three states, and
Does each column of a table in SQL Server have a unique id? I've
I have a table with a Date column. Each Date may appear multiple times.
Suppose I have a tags table with two columns: tagid and contentid . Each
I have a table with a few relational columns and one XML column which
I have an HTML table where each row represents a company and each column
I have created script to find selectivity of each columns for every tables. In
Hi I want to have two tables each have an INT id column which

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.