在PostgreSQL中使用Model方法的Django Query排序不区分大小写

发布于 2021-01-29 14:10:43

我真的是django,python和postgres的新手…仅在使用直接SQL查询的情况下,使用Model作为查询方法时不区分大小写,我似乎无法找到关于order_order的答案。

Model
@classmethod
def get_channel_list(cls, account):
    return cls.objects.filter(accountid=account).order_by('-name').values_list('name', 'channelid')

数据集和排序,目前正在订购中

test
b test
a test channel
a test channel
a test 2 
a b test
Test Channel
Test 3
Test 3
Test 2 Channel

任何帮助将非常感激。

关注者
0
被浏览
99
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    使用QuerySet.extra(select=...)

    @classmethod
    def get_channel_list(cls, account):
        ret = cls.objects.extra(select={'name_lower': 'lower(name)'})
        ret = ret.order_by('-name_lower')
        ret = ret.filter(accountid=account).values_list('name', 'channelid')
        return channels
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看