Django bulk_create函数示例 [英] Django bulk_create function example
本文介绍了Django bulk_create函数示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图了解Django中的bulk_create
I'm trying to understand bulk_create in Django
这是我要转换的原始查询:
This was my original query I'm trying to convert:
for e in q:
msg = Message.objects.create(
recipient_number=e.mobile,
content=batch.content,
sender=e.contact_owner,
billee=batch.user,
sender_name=batch.sender_name
)
这是否意味着执行以下操作(如下)将首先循环并创建所有条目,然后再访问数据库?
Does that mean doing the following (below) will loop and create all the entries first then hit the database? Is this right?
msg = Message.objects.bulk_create({
Message (
recipient_number=e.mobile,
content=batch.content,
sender=e.contact_owner,
billee=batch.user,
sender_name=batch.sender_name
),
})
推荐答案
问题中的第二个代码创建
The second code in the question create a single object, because it pass a set with a Message object.
要创建多个对象,请将多个Message对象传递给bulk_create。例如:
To create multiple objects, pass multiple Message objects to bulk_create. For example:
objs = [
Message(
recipient_number=e.mobile,
content=batch.content,
sender=e.contact_owner,
billee=batch.user,
sender_name=batch.sender_name
)
for e in q
]
msg = Message.objects.bulk_create(objs)
这篇关于Django bulk_create函数示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文