Home > Back-end >  Sort django queryset using string field alphabetically
Sort django queryset using string field alphabetically

Time:10-07

I want to sort users from model with their last names. I am using User.objects.filter(is_active=True).order_by('last_name') Still there is no luck. Pls suggest some solution.

CodePudding user response:

from django.db.models.functions import Lower

User.objects.filter(is_active=True).order_by(Lower('last_name'))

If you want to sort alphabetically you have to use the Lower function. Otherwise, all capitalized words will be at the beginning

  • Related