ben ben - 1 year ago 117
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?

Answer Source

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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download