Search code examples
djangodjango-modelsdjango-querysetpriority-queue

How do I give priority to the user?


I have User database table with status (Int field), timestamp and priority_flag (Int field) fields. I am using the following query to prioritize the user.

users = User.objects.filter(status=status).order_by('priority_flag', 'timestamp')

If the user has high priority_flag, that user should first respond to the query. For example, there are 2 users.

Bob = [priority_flag=Standard, timestamp=datetime.datetime(2007, 2, 17, 23, 42, 31, 584349, tzinfo=<UTC>)]

Alice = [priority_flag=Emergency, timestamp=datetime.datetime(2017, 1, 17, 23, 42, 31, 584349, tzinfo=<UTC>)]

Even if the user Alice is created 10 years later, but her priority is higher than Bob. It should response first. What am I doing wrong here ? Thank you!


Solution

  • If you want to sort by priority_flag in descending order, use '-priority_flag'.

    users = User.objects.filter(status=status).order_by('-priority_flag', 'timestamp')