开发者

in django how do I create a queryset to find double barrel names?

开发者 https://www.devze.com 2023-01-09 04:48 出处:网络
In django, I have a table of people, each of which has a namefirst and namelast. I want to do the sql:

In django, I have a table of people, each of which has a namefirst and namelast.

I want to do the sql:

select * from names where left(namefirst,1)=left(namelast,1).  

Right now my best effort is

qs=People.objects.extra(select={'db':'select left(namefirst,1)=le开发者_如何学编程ft(namelast,1)'})

but then if i stick a .filter(db=1) on that it generates an error.

I suppose I could order by db and just cut it off, but I know there is a better way to do this.


Your extra parameter doesn't look right. You should be using the where parameter (not select):

People.objects.extra(where=['left(namefirst,1)=left(namelast,1)'])
0

精彩评论

暂无评论...
验证码 换一张
取 消