在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第三方库


热门话题
为@Nonnull注释参数编写java单元测试   对于JAVA,如何从它自己的类调用插入排序来将数组分类到一个单独的类中?   Java中嵌套SQL查询的字符串解析   java在所有带有特定注释的方法上调用带注释的方面   在Java中将base64转换为PDF   无法在Android上强制转换java getApplicationContext()   雅加达ee Java ee:如何获取我的应用程序的URL?   IntelliJ:如何导入的文件夹。java文件作为库?   多线程Java(FX)在播放一种声音的同时,播放另一种声音   网络爬虫我需要将proto3版本中protobuf生成的java代码添加到nutch 1.7中   使用JAXRS注释资源的java Restlet客户端   java如何查找给定月份的日期范围   java Minecraft Bukkit插件:我的小游戏传送机不工作   Java Excel搜索和数组列表工作不正常