ben ben - 8 months ago 62
Python Question

Django - How to sort queryset by number of character in a field


name = models.CharField(max_length=255)

I try to sort the queryset. I just think about this:

obj = MyModel.objects.all().sort_by(-len(name)) #???

Any idea?


you might have to sort that in python..

sorted(MyModel.objects.all(),key=lambda o:len(,reverse=True)

or I lied ( A quick google search found the following)