0

I have one of the below example model

class Title(models.Model):
    name = models.CharField(max_length=200)
    provider_name = models.CharField(max_length=255, blank=True)
    keywords = models.CharField(max_length=200, null=True, blank=True)

    def __unicode__(self):
        return '{0} - {1} (name/provider)'.format(name, provider_name)

so in order to order_by the Title model queryset with any model field, we can just do

titles = Title.objects.all().order_by('name')

But is it possible to order_by the queryset with particular value ? i mean i want to order_by the Title model queryset with the return value of unicode method, i.e., the combination of name and provider_name('{0} - {1} (name/provider)'.format(name, provider_name))

So overall instead of doing order_by with Model fields/database columns, i want to order by a value(Return value of unicode method in this case)

Is that possible to order_by the queryset with a value in ORM or else we need to write raw sql in order to achieve this ?

Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
  • 1
    possible duplicate of [Using a Django custom model method property in order\_by()](http://stackoverflow.com/questions/981375/using-a-django-custom-model-method-property-in-order-by) – argaen Jul 02 '15 at 08:02
  • Also, you can write `Title.objects.all().order_by('name')` as `Title.objects.order_by('name')` – Mike Covington Jul 02 '15 at 08:34
  • yep ofcourse, my concern is i want to order_by with value of __unicode__ method ? so how to do it ? – Shiva Krishna Bavandla Jul 02 '15 at 08:38

1 Answers1

0

No, it's not possible to use a method in your filter because the ORM cannot translate it into SQL. In your case, if what you want is to order by name then by provider name, ordering by unicode method (even if it was possible) may give you wrong results since the names are not all the same length.
Using raw SQL for things Django can do is not a good idea either, so I think the best way to do it is:

titles = Title.objects.order_by('name', 'provider_name')
nima
  • 6,566
  • 4
  • 45
  • 57
  • oh i don't want it, because actually the unicode method pasted above was a partial one, in real i was doing extra processing with some if conditions on the current Title model fields, so really want to order the request with the output of unicode method result – Shiva Krishna Bavandla Jul 02 '15 at 09:40
  • In that case, why don't you fetch the data and sort it by unicode in python? – nima Jul 02 '15 at 09:44
  • ok well how to do it in python then and am not sure because i have some gb's of data in that table and will take lot of time right ?? – Shiva Krishna Bavandla Jul 02 '15 at 09:54
  • Yes it might not be efficient for large amounts of data, but this is how you do it anyway :) sorted(Title.objects.all(), key=lambda t: t.__unicode__()) – nima Jul 02 '15 at 13:08
  • I had a similar problem a few years back, but I was using Microsoft tools. I defined my method logic in the DB (SQL Server) as a Stored Procedure and then used that in my queries in Entity Framework (ORM). I have no idea if the same thing can be done in Django but it might be possible. – nima Jul 02 '15 at 13:13