Django Query sort case-insensitive using Model method with PostgreSQL

前端 未结 2 1314
庸人自扰
庸人自扰 2020-12-19 13:35

I\'m really new to django, python and postgres... I can\'t seem to find the answer on how to order_by being case insensitive while using Model as the query method, only if y

2条回答
  •  既然无缘
    2020-12-19 13:52

    Using 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
    

提交回复
热议问题