django-orm 不区分大小写的顺序
我知道,我可以从 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")
而且,我可以获取它们,因为
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)
有没有直接的方法来进行不区分大小写的获取?就像我想要一个序列一样,
# desired sequence: jake, Jake, sulley, Sulley
如果没有,那么建议一个最好的方法来做到这一点。提前致谢。
I know, I can run a case insensitive search from DJango ORM. Like,
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 Django 1.8 开始,可以使用:
https://code.djangoproject.com/ticket/6498
Since Django 1.8 it is possible with:
https://code.djangoproject.com/ticket/6498
这个答案已经过时,请按照 投票最高的解决方案使用 django >= 1.8
我找到了使用的解决方案.extra
原始链接:
http://naorrosenberg.blogspot .fi/2011/04/django-models-orderby-charfield-case.html
This answer is outdated, follow top voted solution with django >= 1.8
I found solution using .extra
original link:
http://naorrosenberg.blogspot.fi/2011/04/django-models-orderby-charfield-case.html
让我们举一个例子,您必须对 User 模型中的字段 "first_name" 进行不区分大小写的排序
Lower 函数将所有将“first_name”值转换为小写,然后进行相应的排序。
Lets take an example where you have to do a case insensitive order by of the field "first_name" from the User Model
The Lower function converts all the "first_name" values into lowercase, and then it's ordered accordingly.