1

i want to display objects in case insensitive order

for eg

class Organization(models.model):
    name=models.CharField(max_length=50)
    administrator = models.ForeignKey(User, blank=True,related_name = 'OrgAdmin')
    types = models.ManyToManyField(OrganizationType,blank=True, null=True)

to display objects ordered by name

Organization.objects.all().order_by('name')

will display in case sensitive

soo what will be option for case insensitive order

how we can achieve this using model method

Ravi
  • 157
  • 2
  • 16

3 Answers3

2

You can also use extra:

Organization.objects.extra(select={'lower_name':'lower(name)'}).order_by('lower_name')

django-orm case-insensitive order by

Edit:

Please check that

Organization.objects.extra(select={'lower_name':'lower(administrator__name)'}).order_by('lower_name')
Community
  • 1
  • 1
Silwest
  • 1,620
  • 1
  • 15
  • 29
  • i tried above with foreign key field Organization.objects.filter().extra(select={'lower_administrator':'lower(administrator)'}‌​).order_by('lower_administrator') gives error OperationalError: no such column: administrator – Ravi Mar 31 '14 at 10:42
0

There's a clear distinction what you should do in model and what in model manager. Basically, models should only contain methods that deal with single instance, while custom model managers should be used to contain any logic that operates on model lists or do custom queries.

So, in your example this should look like:

class OrganizationManager(models.Manager):
    def insensitive(self):
        return self.all().extra(select={'name_lower': 'lower(name)'}, order_by=['name_lower'])

class Organization(models.model):
    objects = OrganizationManager()

And you can use it like

orgs_insensitive = Organization.objects.insensitive()

Refer to django model manager docs for details.

EDIT: turns out django does not support filter field lookups for order, so according to the last comment on this ticket, case insensitive ordering should be done like

my_model.objects.all().extra(select={'imf': 'UPPER(my_field)'}, order_by=['imf'])
J0HN
  • 26,063
  • 5
  • 54
  • 85
  • Could downvoter please explain where exactly am I wrong? – J0HN Mar 31 '14 at 10:11
  • Hey @JOHN thnx, but i tried this, exposing error like FieldError: Cannot resolve keyword u'name_iexact' into field. Choices are:id, name, phone ... etc :( – Ravi Mar 31 '14 at 10:15
  • 1
    This is your typo, not mine :) – J0HN Mar 31 '14 at 10:17
  • what will be query in case of foreign key field ... Organization.objects.all().extra(select={'imf': 'UPPER(administrator)'}, order_by=['imf']) administrator is foreign key here .... OperationalError: no such column: administrator – Ravi Mar 31 '14 at 10:49
  • That could help if you post complete problem the first time. What would be next, `administrator` is calculated field based on user-defined procedure? Aside of that, have you considered using raw sql queries? – J0HN Mar 31 '14 at 11:06
  • actually , m trying the same ... ( with raw ) – Ravi Mar 31 '14 at 11:14
0

it possible to get case insensitive objects based on foreign key attribute (like administrator mentioned above) using raw query.

Organization.objects.raw('select * from organization o,auth_user a on o.administrator_id=a.id order by a.username COLLATE NOCASE ASC')
Ravi
  • 157
  • 2
  • 16