0

i am running the query, but the ordery_by is not working for boolean field value. Any help, would be much appreciated. thank you.

models.py :

class ShopOffer(models.Model):
    offer_title = models.CharField(max_length=120)
    added_by = models.ForeignKey(ShopOwnerShopDetails, on_delete=models.CASCADE)
    is_active = models.BooleanField(default=True)

views.py :

queryset = ShopOffer.objects.filter(added_by__user_id=user).order_by('-is_active')
satyajit
  • 666
  • 1
  • 10
  • 25

2 Answers2

1

Change it as below to get the default ordering false(0) to true(1)

queryset = ShopOffer.objects.filter(added_by__user_id=user).order_by(‘is_active’) 
  • why shouldn't i use order_by for that? Any specific reason? actually my needed is for to sort this data by is active or not. – satyajit Nov 05 '20 at 15:46
  • filter changes number of records you get; ordering (sorting) is for just sorting of filtered set of records – Dmitry Belaventsev Nov 05 '20 at 15:50
  • Actually It's getting the reverse of the data, means first "True" then "False". – satyajit Nov 05 '20 at 15:58
  • Using orderby you get all the queries and then all the data with true are put on the top and false at the bottom or reverse based on whether you use’-‘ or not – prathap sagar Nov 05 '20 at 15:58
  • You’re getting reverse because you have used ‘-is_active’ instead of just ‘is_active’, the “-“ this changes the order to descending order – prathap sagar Nov 05 '20 at 16:22
  • Actually i need to sort the whole queryset as ordey_by from non-active to active. @prathapsagar – satyajit Nov 05 '20 at 16:22
  • Yes remove the “-“ infront of your is_active in the query – prathap sagar Nov 05 '20 at 16:24
  • @prathapsagar '-is_active' means order_by from `False` to `True`. but i'm getting the result from `True` to `False` – satyajit Nov 05 '20 at 16:24
  • https://stackoverflow.com/questions/5408177/linq-order-by-boolean As you can see here the default behaviour of order_by is false to true, you’re reversing it using the “-“ in front of your is_active so remove it, this is default ascending behaviour 0 (false) to 1(true), but adding a “-“ in front of your is_active changes it to descending order, you can read that in the document – https://docs.djangoproject.com/en/3.1/ref/models/querysets/ – prathap sagar Nov 05 '20 at 16:55
1

Most likely in your database that boolean field is stored as TINYINT (or other integer type). That means number 0 will represent False and number 1 will represent True. So when you make ORM call like this:

ShopOffer.objects.filter(added_by__user_id=user).order_by('-is_active')

Your SQL query contains this part:

ORDER BY shop_owner_shopoffer.is_active DESC

What does that mean. That means descending order for integer field. So greater numbers will come first, smaller numbers will come second. So 1 values (True) will come first, 0 values will come second. So True will come first, False will come second.

So if you want False values come first, your query should be like:

ShopOffer.objects.filter(added_by__user_id=user).order_by('is_active')

But once again, it all depends on RDBMS you use. I suggest to check DDL code of the table for ShopOffer table - to see the type of is_active field.

UPDATE1

https://i.stack.imgur.com/sx1g4.png

It's MySQL

UPDATE2

OP confirmed that it's TINYINT field

Dmitry Belaventsev
  • 6,347
  • 12
  • 52
  • 75