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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:08:17+00:00 2026-06-08T23:08:17+00:00

Imagine a model Shirts with a size CharField, with values limited to a small

  • 0

Imagine a model Shirts with a size CharField, with values limited to a small number of choices, e.g. ‘small’, ‘medium’, ‘large’, ‘xlarge’ etc.

To get the shirts grouped by size, you’d do:

Shirts.objects.order_by('size')

But Django will (naturally) order the groups alphabetically, i.e. ‘large’ then ‘medium’ then ‘small’ then ‘xlarge’. What I want is to have ‘small’ before ‘medium’ before ‘large’ etc.

I.e. what I naturally want to do is something like the following pseudocode:

size_order = {'small': 1, 'medium': 2, 'large': 3, 'xlarge': 4}
Shirts.objects.order_by('size_order[size]')

What’s the best way to accomplish this?

EDIT: See my comments to answers below for thoughts on various suggested approaches. I’ve stumbled on a custom Manager/QuerySet approach using the SQL ORDER BY CASE syntax which I’m investigating.

  • 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-08T23:08:18+00:00Added an answer on June 8, 2026 at 11:08 pm

    I figured out the closest thing to what I’m looking for, which is to use QuerySet.extra() method to take advantage of SQL’s CASE WHEN/THEN syntax, which Django doesn’t support directly:

    CASE_SQL = '(case when size="small" then 1 when size="medium" then 2 when size="large" then 3 when size="xlarge" then 4 end)' 
    Shirt.objects.extra(select={'shirt_order': CASE_SQL}, order_by=['shirt_order'])
    

    This may well seem overkill and/or mucky given my (artificial) example, but it’s the trick I was looking for! Thanks to everyone for the other perfectly valid approaches to this problem, which somehow indirectly sparked me to figure out this approach.

    P.S. It’s tempting to create a custom model Manager/QuerySet combo that provides a more native Django-interface for this sort of custom ordering via SQL’s CASE WHEN/THEN syntax, but I’ll leave that as a homework assignment for myself for another time!

    NOTE: The syntax for the CASE WHEN/THEN is database-specific. The syntax above is for SQLite. For PostgreSQL, omit the parentheses and use escaped single quotes instead of double quotes.

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

Sidebar

Related Questions

Imagine a code like this one: @RequestMapping(value=/users, method=RequestMethod.GET) public String list(Model model) { ...
Imagine, I have News models with many text fields class News(models.Model): title = models.CharField(max_length=255)
Imagine a model structure as follows: models/cross_sell_promotion.rb class CrossSellPromotion < Promotion has_and_belongs_to_many :afflicted_products, :join_table
Imagine I have a boolean and a string attribute on my model with data
Imagine a cube-shaped piece of Swiss cheese. We model the cheese through a 20x20x20
Imagine you have this model: class Category(models.Model): node_id = models.IntegerField(primary_key = True) type_id =
Im playing a little bit with heavy-client app. Imagine I have this model: class
Imagine a Model/Collection like: var AModel = Backbone.Model.extend({ defaults: { a: 'a string', b:
My Django-Project is making some strange faned-out GET calls when opening on model from
Imagine I have a model called Course and each course has_many Modules . However

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.