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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:23:29+00:00 2026-06-09T01:23:29+00:00

I want to order my QuerySet in ascending order for this case: PRIORITY_CHOICES =

  • 0

I want to order my QuerySet in ascending order for this case:

PRIORITY_CHOICES = (
(1, "P1"),
(2, "P2"),
(3, "P3"),
(4, "P4"),
(5, "P5"),
)

class Task(models.Model):
     name = models.CharField(max_length=255)
     priority = models.PositiveIntegerField(choices=PRIORITY_CHOICES)

Now, since, the priority can accept None values. The problem is with ordering the Query set when I do this in my View code:

task_list = Task.objects.all().order_by("priority")

This returns me the query set with the objects arranged in ascending order of priority, with the None values first. I want the ascending list but I want to include the None values at the end. I have a huge database so, I am interested in accomplishing this at the database level.

  • 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-09T01:23:30+00:00Added an answer on June 9, 2026 at 1:23 am

    This is probably your best option.

    import itertools
    q = itertools.chain(
            Task.objects.filter(priority__isnull=False).order_by("priority"),
            Task.objects.filter(priority__isnull=True))
    # Then you can iterate over your custom order
    result = [(x.name, x.priority) for x in q]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Message(models.Model): subject = models.CharField(max_length=100) pub_date = models.DateTimeField(default=datetime.now()) class Topic(models.Model): title = models.CharField(max_length=100) message
I have the following models: class Order_type(models.Model): description = models.CharField() class Order(models.Model): type= models.ForeignKey(Order_type)
I have a django model Level for a game. class Level(models.Model): key = models.CharField(max_length=100)
Here is my models.py class Picture(models.Model): image = models.ImageField(upload_to='uploads/') caption = models.CharField(max_length=140, null=True, blank=True)
I have this: class OrderForm(ModelForm): class Meta: model = Order exclude = ('number',) def
I want to have dynamic parameters, so I made these models : class DynamicObject(models.Model):
I want to order by id asc, date asc, but in the case where
I have a table: ID name c_counts f_counts and I want to order all
I need to re-order a Django queryset, because I want to put None values
I have two Django models, roughly summarized by: class Thing(Model): date_created = DateTimeField() class

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.