extra用法

阅读 59

2022-09-17

做子查询时,有些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])

-------------------------------------------

个性签名:代码过万,键盘敲烂!!!

如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!



精彩评论(0)

0 0 举报