I have field in model roll_numb
. Theroll_numb
has values as follows.
070-001
070-007
070-343
080-002
080-008
When
Annotate your queryset with a custom field:
from django.db.models.functions import Substr
YourModel.objects.annotate(roll_split=Substr('roll_numb', 5)).order_by('roll_split')
If you want to always have your models ordered, just move that annotation to the model's manager:
class YourModelManager(models.Manager):
def get_queryset(self):
qs = super(YourModelManager, self).get_queryset()
return qs.annotate(roll_split=Substr('roll_numb', 5)).order_by('roll_split')
class YourModel(models.Model):
objects = YourModelManager()