如何将此Postgresql查询解析为Django

问题描述 投票:0回答:1

我有这个Postgresql查询:

select a.code from accounts a where a.code like '0077-7575%' or '0077-7575' like a.code||'%'

在django,我正在尝试这样做:

q = Accounts.objects.filter(Q(code__startswith='0077-7575') | Q('0077-7575'__startswith=code))

问题是我不知道,也找不到将'0077-7575' like a.code||'%'转换成django的方法,因为a.code是字段名...我该如何解决?

django postgresql
1个回答
0
投票

如果我猜对了(您正在尝试检查您的字段是否包含在'0077-7575'中,则可以尝试使用written in this answer

基本上,您必须做类似的事情:

Accounts.objects \ 
  .annotate(querystring=Value('0077-7575', output_field=CharField())) \ 
  .filter(querystring__contains=F('code'))
© www.soinside.com 2019 - 2024. All rights reserved.