unicode - Order_by queryset with a value in django -
i have 1 of 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 order_by
title model queryset model field, can
titles = title.objects.all().order_by('name')
but possible order_by queryset particular value ? mean want order_by title model queryset return value of unicode
method, i.e., combination of name , provider_name
('{0} - {1} (name/provider)'.format(name, provider_name))
so overall instead of doing order_by model fields/database columns, want order value(return value of unicode method in case)
is possible order_by queryset value in orm or else need write raw sql in order achieve ?
no, it's not possible use method in filter because orm cannot translate sql. in case, if want order name provider name, ordering unicode method (even if possible) may give wrong results since names not same length.
using raw sql things django can not idea either, think best way is:
titles = title.objects.order_by('name', 'provider_name')
Comments
Post a Comment