django-ins insensible a mayúsculas y minúsculas


Lo sé, puedo ejecutar una búsqueda insensible a mayúsculas y minúsculas desde DJango OR. Como,

User.objects.filter(first_name__contains="jake")
User.objects.filter(first_name__contains="sulley")
User.objects.filter(first_name__icontains="Jake")
User.objects.filter(first_name__icontains="Sulley")

Y también, puedo traerlos como

user_list = User.objects.all().order_by("first_name")
# sequence: (Jake, Sulley, jake, sulley)
user_list = User.objects.all().order_by("-first_name") # for reverse
# sequence: (sulley, jake, Sulley, Jake)

¿Hay una forma directa de realizar una búsqueda sin distinción de mayúsculas y minúsculas?? Como en quiero una secuencia como

# desired sequence: jake, Jake, sulley, Sulley

Si no, entonces sugiere la mejor manera de hacerlo. Gracias de antemano.

Author: simplyharsh, 2010-08-04

3 answers

Esta respuesta está desactualizada, consulte la siguiente solución con django 1.8 ->

Encontré la solución usando .extra

class MyModelName(models.Model):
   is_mine = models.BooleanField(default=False)
   name = models.CharField(max_length=100)


MyModelName.objects.filter( is_mine=1 ).extra(\
    select={'lower_name':'lower(name)'}).order_by('lower_name')

Enlace original:

Http://naorrosenberg.blogspot.fi/2011/04/django-models-orderby-charfield-case.html

 23
Author: Troyhy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-24 05:05:08

Desde Django 1.8 es posible con:

from django.db.models.functions import Lower
MyModel.objects.order_by(Lower('myfield'))

Https://code.djangoproject.com/ticket/6498

 51
Author: uri.z,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-02 09:11:56

Esto es para postgresql, pero quizás también sea útil para otras bases de datos:

Http://scottbarnham.com/blog/2007/11/20/case-insensitive-ordering-with-django-and-postgresql/

 6
Author: gruszczy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-04 19:37:37