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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:51:30+00:00 2026-06-17T08:51:30+00:00

I need to sort a queryset for a Django admin change list view by

  • 0

I need to sort a queryset for a Django admin change list view by two different factors: order_time and status.

When sorted the resulting queryset should be ordered by decreasing order_time with all items that have (item.status == 3) at the bottom of the list. Essentially I’m trying to push all completed orders to the bottom of the list regardless of their order_time.

This is what the model looks like:

def Order(models.Model);
  order_time = models.DateTimeField(null=True)
  status = models.PositiveSmallIntegerField(choices=ORDER_STATUS, default=ORDER_RECEIVED)

I’ve messed around with combining querysets with itertools.chain but since I’m doing this for a django change list view I specifically need to return a queryset.

Could I utilize Managers for this? Or is their a way to execute custom SQL that preorders the qs accordingly?

Here’s an example of what the order queryset should look like:

ORDER    ORDER_TIME      STATUS
  3         12/3           2
  8         12/5           2
  1         12/6           1
  7         12/2           3
  10        11/12          3 
  • 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-17T08:51:31+00:00Added an answer on June 17, 2026 at 8:51 am

    How about making a model for the choices.

    def Status(models.Model):
        # You could put in an 'id' or 'code' field to match up to your current status codes properly
        value = models.CharField(max_length=20)
        importance = models.IntegerField()
    

    Then alter your model to:

    class Order(models.Model);
        order_time = models.DateTimeField(null=True)
        status = models.ForeignKey('Status')
    
        class Meta:
            ordering = ('status__importance', 'order_time')
    

    Create your status models and set the importance field as required, in your example set the importance of status 1 & 2 to be equal (eg 5) and set the importance of status 3 to be “heavier” (eg 10). This will group order by the importance, then the time, so statuses with the same importance will be grouped together.

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

Sidebar

Related Questions

I need to sort list of strings in the alphabetical order: List<String> list =
I need to sort a coordinate list for a rectangle counterclockwise, and make the
I need to sort an employee list based on predefined uniqueIds. In simple words,
I need to sort my view by a specific field that is actually a
I need to sort the list in java as below: List contains collection of
I need to sort object list using comparators I need nested type sorting such
I need to sort a List<string> which contains Japanese alphabet. How could I do
I need to sort a list, by X and Y. I read some on
I need to sort a List based on the difference between the strings in
I need to sort a shopping list by the aisle the item is located

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.