在django orm上使用一个查询进行批量更新。

django-bulk-update的Python项目详细描述


django批量更新[构建状态](https://travis-ci.org/aykut/django-bulk-update.svg?branch=master)(https://travis ci.org/aykut/django批量更新)
[![覆盖状态](https://coveralls.io/repos/aykut/django-bulk-update/badge.svg?branch=master)(https://coveralls.io/r/aykut/django-bulk-update?branch=master)


===
==
与管理器:

`` python
从django_bulk_update导入random
。管理器从测试导入bulk update manager
。模型导入person


类person(models.model):

对象=bulkupdatemanager()

random_names=['walter','the dude','donny','jesus']
people=person.objects.all()
对于person-in-people:
person.name=random.choice(random廑names)

person.objects.bulk廑update(people,update廑fields=['name'])仅更新name列
person.objects.bulk update(people,exclud廑fields=['username'])更新除username
person.objects.bulk_update(people)之外的所有列更新所有列
person.objects.bulk_update(people,batch_size=50000)更新所有列,按50000大小的块进行更新
``````



om django_bulk_update.helper从测试导入bulk_update
。models导入person

random_names=['walter','the dude','donny','jesus']
people=person.objects.all()
对于person-in-people:
person.name=random.choice(random_names)

bulk update(people,update_fields=['name'])仅更新名称列
大容量更新(people,exclude懔fields=['username'])更新除username以外的所有列
大容量更新(people,using='someotherdb')使用给定的db更新所有列
大容量更新(people)使用默认db更新所有列
大容量更新te(people,batch戆size=50000)使用默认的db
````

注意:您可以考虑在只想更新"name"时使用`.only('name')`,这样django将只从db检索名称数据。

并考虑使用`.defer('username')`w当您不想更新"username"时,Django将不会从数据库中检索username。
这些优化可以进一步提高性能。


性能测试:
`.save()`在每次对象更新时(`dmmy_update`)。有趣的指标是使用"bulk\u update"函数的速度比实际原始时间快。



``python
jango

在[4]:os.environ['django_settings_module']="tests.test_settings'
在[5]:django.setup()


在[6]:from tests.fixtures import在[7]:django.db.connection.creation.create_test_db()
在[8]:create_fixtures(1000)


在[9]:setup=''
/>从django_bulk_导入random
从tests.models导入person
random_names=['walter','the dude','donny','jesus']
ids=list(person.objects.values_list('id',flat=true)[:1000])
people=person.objects.filter(id_in=ids)
对于p in people:
name=random.choice(random_name s)
p.name=name
p.email='%s@example.com'%name
bu update=lambda:helper.bulk_update(people,update_fields=['name','email'])
'

[10]:bu perf=min(timeit.timer('bu_update()',setup=setup)。重复(7,100))

[11]:setu"="
import random
from tests.models import person
from django.db.models import f
random\u names=['walter','the dude','donny','jesus']
ids=list(person.objects.values\u list('id',flat=true)[:1000])
people=person.objects.filter(id\u in=ids)
def dmmy\u update():
p在人物中:
name=random.choice(random_name s)
p.name=name
p.email=''%s@example.com'%name
p.save(update_fields=['name','email'])
'

rint"批量更新性能:%.2f。虚拟更新性能:%.2f。加速:%.2f。"%(bu-perf,dmmy-perf,dmmy-perf/bu-perf)
批量更新性能:7.05。虚拟更新性能:373.12。提速:52.90。
`````


要求
============================================================================================================================================================================(https://github.com/daleobrien)
-[sruon](https://github.com/sruon)
-[豪厄尔](https://github.com/hoverhell)
-[c-nichols](https://github.com/c-nichols)
-[towr](https://github.com/towr)
-[joshblum](https://github.com/joshblum)
-[luzfcb](https://github.com/luzfcb)
-[torchinsolm](https://github.com/torchinsolm)
-[cihann](https://githu)b.com/cihann)
-[wetneb(https://github.com/wetneb)
-[tatterdemalion(https://github.com/tatterdemalion)
-[gabriel laet(https://github.com/gabriel laet)
-[arna126(https://github.com/arna126)

todo
-[arna126(https://github.com/arna126)(https://github.com/arnaau126)(https://github.com/arnaau126)



todo
todo
==========-几何字段支持

根据MIT许可证发布Django批量更新。有关详细信息,请参见许可证文件。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java访问私有字段而不使用getter方法?   使用PowerMockito在JavaEWSAPI中模拟测试拉订阅   启动活动时未保存java首选项并清除变量   java如何在servlet中检索子域?斯普林有帮手吗   java使用Docker从命令行构建Android项目   java Android,ActionBar后退按钮(setDisplayHomeAsUpEnabled(true))重新创建父活动   java在重用FileOutputStream时应该关闭流吗?   java使用RESTAPI将文件上载到s3 bucket   Java SOAP Web服务应用程序中的mysql用户登录方法不工作   java使用多个数字计算百分比并转换为长   java Android SQLiteDatabase查询忽略空格   java如何在Javafx中比较两个字段文本   java错误:未设置java_HOME,在Eclipse安装后找不到   java在安卓中保存对象   java如何使用jaxws从返回List<Object>的服务中检索值   java Google OAuth2 JWT令牌验证异常   SpringMVC中的JavaUTF8编码问题,当从JSP表单发送POST请求中的越南语信件时   java从webview重定向到安卓应用程序   JUnit 5中多个扩展的java顺序