extra用法

做子查询时,有些orm语句满足不了的时候使用

select参数

## select age,(age > 18) as is_adult from myapp_person;
Person.object.all().extra(select={'is_adult':"age>18"})

where参数

## select * from myapp_person where first||last |like 'jeffrey%'.
Person.objects.all().extra(where=["first||last|LIKE 'jeffrey%'"])

table连接其他表

# select * from myapp_book,myapp_person where last=author_last
Book.objects.all().extra(table=['myapp_person'], where=['last=author_last'])

防止sql注入

# 错误方式
first_name = 'joe'
Person.objects.all().extra(where=["first='%s'" % first_name])
# 正确方式
Person.objects.all().extra(where=["first='%s'"], params=[first_name])
posted @ 2022-09-14 23:13  我在路上回头看  阅读(115)  评论(0编辑  收藏  举报