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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:12:12+00:00 2026-06-11T03:12:12+00:00

I am trying to order a Django QuerySet by whether a model field value

  • 0

I am trying to order a Django QuerySet by whether a model field value is in a list.

I know that you can filter on this property, like this:

my_qs = MyModel.objects.filter(id__in=[1,3,5,11])

will give me only the MyModel objects with ids 1, 3, 5, and 11.

I’d like to order on this property. Using the above example, I’d like have the MyModel objects with ids in the list first, but still have all the other MyModel objects too, just after them.

Thanks for your 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-06-11T03:12:14+00:00Added an answer on June 11, 2026 at 3:12 am
    my_qs = MyModel.objects.filter(id__in=[1,3,5,11])
    not_my_qs = MyModel.objects.exclude(id__in=[1,3,5,11])
    
    from itertools import chain
    result_list = list(chain(my_qs, not_my_qs))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to order models in a Django app using a field that has
I'm trying to solve the relational model in order to make a Django app.
I am trying to create a Django site that shows a list of products,
I am trying to add an additional custom field to a django model. I
I'm using django/apache/sqlite3 and I have a django model that looks like this: class
I am trying to write a higher-order Racket function that takes a first-order function
In a Django UserProfile model, I am trying to do the following: class UserProfile(models.Model):
I've been trying to configure two separate Django apps on one server such that
I'm using django-taggit to tag items in a todo list app. I'm trying to
I know this is a very basic concept in Django, and I have tried

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.