Opened 8 years ago

Closed 7 months ago

#6498 closed New feature (fixed)

Add case insensitive model ordering

Reported by: Bastian Kleineidam <calvin@…> Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: gav@…, mmitar@…, garrypolley Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by ramiro)

Add support for case insensitive ordering of model objects by using
.order_by("*name") or .order_by("*-name") syntax.

Attachments (1)

0045-Add-case-insensitive-model-ordering.patch (4.9 KB) - added by Bastian Kleineidam <calvin@…> 8 years ago.

Download all attachments as: .zip

Change History (25)

Changed 8 years ago by Bastian Kleineidam <calvin@…>

comment:1 Changed 8 years ago by PJCrosier

  • Needs documentation set
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 8 years ago by jacob

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 7 years ago by ramiro

  • Description modified (diff)

comment:4 Changed 7 years ago by gav

  • Cc gav@… added

comment:5 Changed 7 years ago by anonymous

  • milestone set to post-1.0

comment:6 Changed 7 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:7 Changed 6 years ago by thejaswi_puthraya

  • Component changed from Uncategorized to Database layer (models, ORM)

comment:8 Changed 5 years ago by julien

  • Type set to New feature

comment:9 Changed 4 years ago by julien

  • Severity set to Normal

comment:10 Changed 4 years ago by Alex

  • Easy pickings unset
  • Triage Stage changed from Design decision needed to Accepted
  • UI/UX unset

The idea of case insensitive ordering is accepted, some more thought should be put into the API, * for case-sensitive isn't particularly intuitive.

comment:11 Changed 4 years ago by Alex

I've closed #7580 as a dupe of this. This should be used to discuss a general way to order by alternate things.

comment:12 Changed 4 years ago by mitar

  • Cc mmitar@… added

comment:13 Changed 3 years ago by anonymous

¿What about a parameter like 'iexact'?

comment:14 Changed 2 years ago by garrypolley

Any reason this can't just work like this:


I don't think that would cause any issues, it'll just be a bit of a chore to get it added to all the backends.

Version 0, edited 2 years ago by garrypolley (next)

comment:15 Changed 2 years ago by garrypolley

I'm thinking of doing this for the DjangoCon sprint, will it get accepted if I do it?

My proposal is still the same


This would also include a doc update about the change.

comment:16 Changed 2 years ago by garrypolley

I'd expect this python


to become this SQL

SELECT * FROM my_model ORDER BY LOWER(my_field);  

I'm also fine using UPPER, I'm not too particular either way. Seems most people in raw SQL prefer UPPER.

comment:17 Changed 2 years ago by aaugustin

I don't find my_field__iexact much better than the original proposal of '*my_field'... Try grabbing a core dev or two at the sprints to get a design decision on the API :)

Otherwise, the most difficult part is to get this working on all four databases supported by Django. Yes, this includes Oracle :) (I have the infrastructure to test it if you don't and I'll be a the sprints tomorrow.)

comment:18 Changed 2 years ago by akaariai

If custom lookups supports gets added, doing this for your project will be much easier. See for an example.

Of course, there is that if above...

comment:19 Changed 2 years ago by garrypolley

  • Cc garrypolley added

comment:20 follow-up: Changed 2 years ago by shai

I believe my_model.objects.all().extra(select={'imf': 'UPPER(my_field)'}, order_by=['imf']) should work on all core backends.

I am not sure a dedicated API is justified.

comment:21 in reply to: ↑ 20 Changed 18 months ago by anonymous

what about in case of foreign key field .... ?

comment:22 Changed 18 months ago by mjtamlyn

A dedicated API for this is likely not justified, but we should be able to order by any Transform which can be applied to the field. At the moment, case insensitive searches are not done as a Transform, although it would be easy to create an __upper transform and use __iexact as a proxy to it (also __icontains would proxy to __upper__contains for example).

comment:23 Changed 7 months ago by JanMalte

Any news about case insensitive ordering?

comment:24 Changed 7 months ago by timgraham

  • Resolution set to fixed
  • Status changed from new to closed

Yes, I think it's addressed in Django 1.8 with the ability to order_by() expressions. For example:

>>> from django.db.models.functions import Lower
>>> MyModel.objects.order_by(Lower('myfield'))
Note: See TracTickets for help on using tickets.
Back to Top