django-orm不区分大小写,

时间:2021-01-14 19:23:50

I know, I can run a case insensitive search from DJango ORM. Like,

我知道,我可以对DJango ORM进行不敏感的搜索。就像,

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")

And also, I can fetch them as

而且,我可以把它们拿来。

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)

Is there a direct way for a case-insensitive fetch?? As in I want a sequence as

对于不区分大小写的取回有直接的方法吗?我想要一个序列

# desired sequence: jake, Jake, sulley, Sulley

If not, then suggest a best way to do it. Thanks in advance.

如果没有,那么建议一个最好的方法。提前谢谢。

3 个解决方案

#1


23  

This answer is outdated, check below solution with django 1.8 ->

这个答案已经过时了,请查看django 1.8 ->的解决方案

I found solution using .extra

我用。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')

original link:

原来的链接:

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

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

#2


47  

Since Django 1.8 it is possible with:

因为Django 1.8是可能的:

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

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

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

#3


6  

This is for postgresql, but maybe it will be useful for other databases too:

这是针对postgresql的,但它也可能对其他数据库有用:

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

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

#1


23  

This answer is outdated, check below solution with django 1.8 ->

这个答案已经过时了,请查看django 1.8 ->的解决方案

I found solution using .extra

我用。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')

original link:

原来的链接:

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

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

#2


47  

Since Django 1.8 it is possible with:

因为Django 1.8是可能的:

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

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

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

#3


6  

This is for postgresql, but maybe it will be useful for other databases too:

这是针对postgresql的,但它也可能对其他数据库有用:

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

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