Reputation: 25
I was working on a project and I came across a problem. I need a query_set which should have ordering according to created date but there are some items in the query_set which I need to place at the end of the query_set at all times.
I don't need to apply ordering for those items. Those items should be always at the end of the query_set
Here is my model
class Bookings(model.Model):
booking_title = models.CharField('Booking title', max_length=125, blank=True, null=True)
user = models.ForeignKey(User, related_name='request_user', on_delete=models.CASCADE)
practice = models.BooleanField(default=False)
status = models.IntegerField(choices=[1, 2], default=1)
created_date = models.DateTimeField(auto_now_add=True)
Here is my query_set
query_set = Bookings.objects.filter(status=1)
Here is my solution (Which is don't work)
query_set_practice = query_set.filter(practice=True)
query_set_normal = query_set.filter(practice=False).order_by('created_date')
query_set = query_set_normal | query_set_practice
I need normal bookings first and practice bookings. The normal bookings should be in the order (last should be first).
I need an optimized solution without adding any loops and multiple query_set creations.
Please help me.
Upvotes: 1
Views: 49
Reputation: 4095
Change this to:
query_set_normal = query_set.filter(practice=False).order_by('-created_date')
Upvotes: 0