Code

Opened 6 years ago

Last modified 3 months ago

#6498 new New feature

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@…> 6 years ago.

Download all attachments as: .zip

Change History (23)

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

comment:1 Changed 6 years ago by PJCrosier

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

comment:2 Changed 6 years ago by jacob

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 6 years ago by ramiro

  • Description modified (diff)

comment:4 Changed 6 years ago by gav

  • Cc gav@… added

comment:5 Changed 6 years ago by anonymous

  • milestone set to post-1.0

comment:6 Changed 5 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:7 Changed 5 years ago by thejaswi_puthraya

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

comment:8 Changed 3 years ago by julien

  • Type set to New feature

comment:9 Changed 3 years ago by julien

  • Severity set to Normal

comment:10 Changed 3 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 3 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 2 years ago by mitar

  • Cc mmitar@… added

comment:13 Changed 18 months ago by anonymous

¿What about a parameter like 'iexact'?

comment:14 Changed 12 months ago by garrypolley

Any reason this can't just work like this:

my_model.objects.all().order_by(my_field__iexact)

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 12 months ago by garrypolley (next)

comment:15 Changed 10 months 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

my_model.objects.all().order_by('my_field__iexact')

This would also include a doc update about the change.

comment:16 Changed 10 months ago by garrypolley

I'd expect this python

my_model.objects.all().order_by('my_field_iexact')

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 10 months 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 10 months ago by akaariai

If custom lookups supports gets added, doing this for your project will be much easier. See https://github.com/akaariai/django/commit/89c4765044787ca8084541da0c31c2d47956c720#L1R73 for an example.

Of course, there is that if above...

comment:19 Changed 10 months ago by garrypolley

  • Cc garrypolley added

comment:20 follow-up: Changed 10 months 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 3 months ago by anonymous

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

comment:22 Changed 3 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).

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.