加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用PostgreSQL的Model方法,Django查询排序不区分大小写

发布时间:2020-12-13 16:29:49 所属栏目:百科 来源:网络整理
导读:我是django,python和postgres的新手……我似乎无法找到关于如何在使用Model作为查询方法时不区分大小写的order_by的答案,只有当你使用直接SQL查询时. Model@classmethoddef get_channel_list(cls,account): return cls.objects.filter(accountid=account).or
我是django,python和postgres的新手……我似乎无法找到关于如何在使用Model作为查询方法时不区分大小写的order_by的答案,只有当你使用直接SQL查询时.
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

任何帮助将非常感激.

使用 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读