Order_by is a commonly used django query to order the queryset based on the order_by field name. Basic syntax of order_by is shown below.
MyModel.objects.all().order_by('field_name')
Consider the following models.py
from django.db import models
class SelectedProducts(models.model):
'''
Model for storing the product basic updation details.
'''
name = models.CharField(max_length=250)
price = models.FloatField(default=0.0)
count = models.IntegerField(blank=True, null=True, max_length=5)
def __unicode__(self):
return self.name
Now consider the case where you have 3 entries in your table.Name Price Count
abc 15.0 5
lmo 10.25 2
xyz 12.50 (None)
According to the following query:
SelectedProducts.objects.all().order_by('count')
Output:
[ <objects:xyz>, <objects:lmo>, <objects:abc>]
Since the count of xyz is None, it will be shown first.
Now, to show model instance with count 'None' at the end, you can use following methods:
Output:
[ <objects:lmo>, <objects:abc>, <objects:xyz>]
In some cases the above approach returns sql syntax error.
[ <objects:xyz>, <objects:lmo>, <objects:abc>]
Since the count of xyz is None, it will be shown first.
Now, to show model instance with count 'None' at the end, you can use following methods:
1. Method: Using 'extra'
Based on this method code will be:
selected = SelectedProducts.objects.all().order_by('count')
q = selected.extra(select={'count_null': 'count is null'})
q = q.extra(order_by=['count_null'])
print q
Output:
[ <objects:lmo>, <objects:abc>, <objects:xyz>]
In some cases the above approach returns sql syntax error.
2. Method: Using 'aggregate'
Based on this method code will be:
[ <objects:lmo>, <objects:abc>, <objects:xyz>]
from django.db.models import Count
q = SelectedProducts.objects.all().annotate(null_count=Count('count')).order_by('-null_count', 'count')
print q
Output:[ <objects:lmo>, <objects:abc>, <objects:xyz>]