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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:15:20+00:00 2026-05-13T10:15:20+00:00

Consider this sample model: MODEL_CHOICES = ( (uSPAM, uSpam), (uXOUP, uEggsoup), ) (snip) type

  • 0

Consider this sample model:

MODEL_CHOICES = (
(u”SPAM”, u”Spam”),
(u”XOUP”, u”Eggsoup”),
)

(snip)

type = models.CharField(max_length=4, choices=MODEL_CHOICES)

(The actual implementation is domain-specific and in non-English, so this sample has to do.)

When building my query, I’d like to sort the results by that type field, and present the results to the user. Naturally, I’d like to sort by the display name of that field.

Something along the lines of:

documents = MyModel.objects.filter(…).order_by(“type”)

However, [query-set].order_by([field]) only allows sorting by field name, which will result in SPAM < XOUP (to the machine) even though Eggsoup < Spam (to the human reader).

Consider this set of instances sorted by type:

Name | Type

obj1 | SPAM

obj2 | SPAM

obj3 | SPAM

obj4 | XOUP

obj5 | XOUP

But this is the order the user will see, i.e. the user will see the display name, not the internal field value of the type column:

Name | Type

obj1 | Spam

obj2 | Spam

obj3 | Spam

obj4 | Eggsoup

obj5 | Eggsoup

Which in the eyes of the human user is not sorted correctly.

Is there a feature in Django that allows sorting by display name? Or a way to accomplish this “manually”? (Renaming the choices so that the display name has the same order as the actual values is not an option.)

  • 1 1 Answer
  • 1 View
  • 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-13T10:15:20+00:00Added an answer on May 13, 2026 at 10:15 am

    If the result sets are small enough, you could do an in-memory sort of results in code.
    I can’t think of any decent way to sort the results in database level. It certainly would be possible if you used stored procedure which is aware of display names, however you’d have to write raw sql.
    As for manual sorting- you can use something like this:

    obj_list = list(Model.objects.all())
    import operator
    obj_list.sort(key=operator.methodcaller('get_foo_display'))
    

    Here are some nice examples on how to sort lists in Python: http://wiki.python.org/moin/HowTo/Sorting

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

Sidebar

Related Questions

Consider the models: class Author(models.Model): name = models.CharField(max_length=200, unique=True) class Book(models.Model): pub_date = models.DateTimeField()
Consider this sample code: <div class=containter id=ControlGroupDiv> <input onbeforeupdate=alert('bingo 0'); return false; onclick=alert('click 0');return
First consider this sample C++ code: std::string input1, input2, input3; std::cout << Enter Input
Is there an SQL injection possibility even when using mysql_real_escape_string() function? Consider this sample
Consider a simple table with an auto-increment column like this: CREATE TABLE foo (
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {
Consider this case: dll = LoadDLL() dll->do() ... void do() { char *a =
Consider this Python program which uses PyGtk and Hippo Canvas to display a clickable
Consider this: public class TestClass { private String a; private String b; public TestClass()

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.