python-3.x - 如何在 Django 的一个查询中保存所有模型更改
问题描述
我尝试修改某些模型(如User
模型)的许多实例,并且这种更改是不同的(我不想使用更新QuerySet
方法并且不适用于我的场景)。
例如,有些用户需要更改first_name
,有些用户需要更改last_name
并获得以下用户:all_user = User.objects.all()
我想如果我在更改后对每个实例使用保存方法,Django 会发送一个查询来保存它!
如何在一个查询中保存对数据库的所有更改,而不是在模型上使用 foreach 并一一保存?
解决方案
鉴于@iklinac 的评论,我会彻底推荐实施 django 自己的批量更新方法,这里详细介绍
这与我在下面的原始答案非常相似,但看起来该功能现在已内置。
# bulk_update(objs, fields, batch_size=None)
>>> objs = [
... Entry.objects.create(headline='Entry 1'),
... Entry.objects.create(headline='Entry 2'),
... ]
>>> objs[0].headline = 'This is entry 1'
>>> objs[1].headline = 'This is entry 2'
>>> Entry.objects.bulk_update(objs, ['headline'])
原始答案
有一个名为django-bulk-update的包,它类似于 django 内置的 bulk create。
我使用它的一个例子是管理类中操作的一部分;
@admin.register(Token)
class TokenAdmin(admin.ModelAdmin):
list_display = (
'id',
'type'
)
actions = (
'set_type_charity',
)
def set_type_charity(self, request, queryset):
for token in queryset:
token.type = Token.Type.CHARITY
bulk_update(
queryset,
update_fields=['type', 'modified'],
batch_size=1000
)
用法,取自他们的自述文件;
与经理:
import random
from django_bulk_update.manager import BulkUpdateManager
from tests.models import Person
class Person(models.Model):
...
objects = BulkUpdateManager()
random_names = ['Walter', 'The Dude', 'Donny', 'Jesus']
people = Person.objects.all()
for person in people:
person.name = random.choice(random_names)
Person.objects.bulk_update(people, update_fields=['name']) # updates only name column
Person.objects.bulk_update(people, exclude_fields=['username']) # updates all columns except username
Person.objects.bulk_update(people) # updates all columns
Person.objects.bulk_update(people, batch_size=50000) # updates all columns by 50000 sized chunks
有帮手:
import random
from django_bulk_update.helper import bulk_update
from tests.models import Person
random_names = ['Walter', 'The Dude', 'Donny', 'Jesus']
people = Person.objects.all()
for person in people:
person.name = random.choice(random_names)
bulk_update(people, update_fields=['name']) # updates only name column
bulk_update(people, exclude_fields=['username']) # updates all columns except username
bulk_update(people, using='someotherdb') # updates all columns using the given db
bulk_update(people) # updates all columns using the default db
bulk_update(people, batch_size=50000) # updates all columns by 50000 sized chunks using the default db
推荐阅读
- excel - 范围对象上的 Excel VBA 错误 1004 使用范围为工作簿级别的范围名称
- angular - 发送 JSON 时,sendcommandtodevice Google Cloud IoT 失败
- jquery - 将字符串值排序到数据表中日期类型列的表底部
- excel - 计算括号内的字符
- mysql - 如何使mysql客户端提示反映您连接的服务器,而不是本地服务器
- apache-camel - 通过骆驼拆分器和结束拆分器获取文件中的第一行
- c++ - 检查两个图是否互补的函数
- ibm-odm - IBM ODM:如何定义规则以仅验证字符串中的数值
- matplotlib - 如何在 Octave 中运行 wget
- javascript - 无法检索嵌套返回值