有时候我们需要同时(一次性)更新某个用户的多个字段。

运行环境 Runtime environment

1
2
3
4
5
操作系统 : Ubuntu18.04
IDE: JetBrains pycharm 2019.2.4 x64
Python: 3.6.9
Django: 3.0.5
djangorestframework: 3.11.0

背景

有时候我们需要同时(一次性)更新某个用户的多个字段。

一个个属性的去修改保存太麻烦了,但是架不住django的ORM就是厉害。

项目应用下models.py

1
2
3
4
5
6
7
8
9
class magnetInfo(models.Model):
mtitle = models.CharField(max_length=255, verbose_name='名称', unique=True)
mdate = models.DateTimeField(verbose_name='发布日期', null=True)
mpublisher = models.CharField(max_length=255, default="未知", verbose_name='发布人', null=True)
magnet = models.TextField(verbose_name='磁性链接', null=True)
magnetSpare = models.TextField(verbose_name='备用磁性链接', null=True)
msize = models.CharField(max_length=255, verbose_name='文件大小', null=True)
morigin = models.CharField(max_length=255, verbose_name='网站来源', null=True)
murl = models.URLField(verbose_name='源链接', null=True)

磁性链接数据

1
2
3
4
5
6
7
8
9
10
{
"mtitle": "物语系列合集(化物语 伪物语 猫物语(黑) 猫物语(白) 倾物语 囮物语 鬼物语 恋物语 花物语 凭物语 终物语 历物语 伤物语 终物语第二季 续·终物语) Monogatari Series [10bit BD1080p][HEVC-x265] 内详",
"mdate": "2019-07-29 14:18",
"mpublisher": "ReinForcee",
"magnet": "magnet:?xt=urn:btih:TM4GWRHXKCTS3LGB7LVS6NKBV5GWH4MT&dn=&tr=http%3A%2F%2F104.238.198.186%3A8000%2Fannounce&tr=udp%3A%2F%2F104.238.198.186%3A8000%2Fannounce&tr=http%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker3.itzmx.com%3A6961%2Fannounce&tr=http%3A%2F%2Ftracker4.itzmx.com%3A2710%2Fannounce&tr=http%3A%2F%2Ftracker.publicbt.com%3A80%2Fannounce&tr=http%3A%2F%2Ftracker.prq.to%2Fannounce&tr=http%3A%2F%2Fopen.acgtracker.com%3A1096%2Fannounce&tr=https%3A%2F%2Ft-115.rhcloud.com%2Fonly_for_ylbud&tr=http%3A%2F%2Ftracker1.itzmx.com%3A8080%2Fannounce&tr=http%3A%2F%2Ftracker2.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker1.itzmx.com%3A8080%2Fannounce&tr=udp%3A%2F%2Ftracker2.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker3.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker4.itzmx.com%3A2710%2Fannounce&tr=http%3A%2F%2Fnyaa.tracker.wf%3A7777%2Fannounce&tr=http%3A%2F%2Fanidex.moe%3A6969%2Fannounce&tr=http%3A%2F%2Ftracker.anirena.com%3A80%2Fannounce",
"magnetSpare": "magnet:?xt=urn:btih:TM4GWRHXKCTS3LGB7LVS6NKBV5GWH4MT&dn=&tr=http%3A%2F%2F104.238.198.186%3A8000%2Fannounce&tr=udp%3A%2F%2F104.238.198.186%3A8000%2Fannounce&tr=http%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker3.itzmx.com%3A6961%2Fannounce&tr=http%3A%2F%2Ftracker4.itzmx.com%3A2710%2Fannounce&tr=http%3A%2F%2Ftracker.publicbt.com%3A80%2Fannounce&tr=http%3A%2F%2Ftracker.prq.to%2Fannounce&tr=http%3A%2F%2Fopen.acgtracker.com%3A1096%2Fannounce&tr=https%3A%2F%2Ft-115.rhcloud.com%2Fonly_for_ylbud&tr=http%3A%2F%2Ftracker1.itzmx.com%3A8080%2Fannounce&tr=http%3A%2F%2Ftracker2.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker1.itzmx.com%3A8080%2Fannounce&tr=udp%3A%2F%2Ftracker2.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker3.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker4.itzmx.com%3A2710%2Fannounce&tr=http%3A%2F%2Fnyaa.tracker.wf%3A7777%2Fannounce&tr=http%3A%2F%2Fanidex.moe%3A6969%2Fannounce&tr=http%3A%2F%2Ftracker.anirena.com%3A80%2Fannounce",
"msize": "29.1GB",
"morigin": "dmhy",
"murl": "https://share.dmhy.org/?keyword=%E9%BB%91%E7%8C%ABtopics/view/521371_Monogatari_Series_10bit_BD1080p_HEVC-x265.html"
}

项目应用下views.py视图文件

1
2
# 太长我就省略了
magnetInfo.object.create(mtitle='物语系列合集(化物语 伪物语 猫物语(黑) 猫物语(白)..'.....)

用于更新的数据字典

1
2
3
4
5
6
7
8
9
10
up_info = {
"mtitle": "物语系列合集(化物语 伪物语 猫物语(黑) 猫物语(白) 倾物语 囮物语 鬼物语 恋物语 花物语 凭物语 终物语 历物语 伤物语 终物语第二季 续·终物语) Monogatari Series [10bit BD1080p][HEVC-x265] 内详",
"mdate": "2019-07-29 14:18",
"mpublisher": "ReinForcee",
"magnet": "magnet:?xt=urn:btih:TM4GWRHXKCTS3LGB7LVS6NKBV5GWH4MT&dn=&tr=http%3A%2F%2F104.238.198.186%3A8000%2Fannounce&tr=udp%3A%2F%2F104.238.198.186%3A8000%2Fannounce&tr=http%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker3.itzmx.com%3A6961%2Fannounce&tr=http%3A%2F%2Ftracker4.itzmx.com%3A2710%2Fannounce&tr=http%3A%2F%2Ftracker.publicbt.com%3A80%2Fannounce&tr=http%3A%2F%2Ftracker.prq.to%2Fannounce&tr=http%3A%2F%2Fopen.acgtracker.com%3A1096%2Fannounce&tr=https%3A%2F%2Ft-115.rhcloud.com%2Fonly_for_ylbud&tr=http%3A%2F%2Ftracker1.itzmx.com%3A8080%2Fannounce&tr=http%3A%2F%2Ftracker2.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker1.itzmx.com%3A8080%2Fannounce&tr=udp%3A%2F%2Ftracker2.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker3.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker4.itzmx.com%3A2710%2Fannounce&tr=http%3A%2F%2Fnyaa.tracker.wf%3A7777%2Fannounce&tr=http%3A%2F%2Fanidex.moe%3A6969%2Fannounce&tr=http%3A%2F%2Ftracker.anirena.com%3A80%2Fannounce",
"magnetSpare": "magnet:?xt=urn:btih:TM4GWRHXKCTS3LGB7LVS6NKBV5GWH4MT&dn=&tr=http%3A%2F%2F104.238.198.186%3A8000%2Fannounce&tr=udp%3A%2F%2F104.238.198.186%3A8000%2Fannounce&tr=http%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker3.itzmx.com%3A6961%2Fannounce&tr=http%3A%2F%2Ftracker4.itzmx.com%3A2710%2Fannounce&tr=http%3A%2F%2Ftracker.publicbt.com%3A80%2Fannounce&tr=http%3A%2F%2Ftracker.prq.to%2Fannounce&tr=http%3A%2F%2Fopen.acgtracker.com%3A1096%2Fannounce&tr=https%3A%2F%2Ft-115.rhcloud.com%2Fonly_for_ylbud&tr=http%3A%2F%2Ftracker1.itzmx.com%3A8080%2Fannounce&tr=http%3A%2F%2Ftracker2.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker1.itzmx.com%3A8080%2Fannounce&tr=udp%3A%2F%2Ftracker2.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker3.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker4.itzmx.com%3A2710%2Fannounce&tr=http%3A%2F%2Fnyaa.tracker.wf%3A7777%2Fannounce&tr=http%3A%2F%2Fanidex.moe%3A6969%2Fannounce&tr=http%3A%2F%2Ftracker.anirena.com%3A80%2Fannounce",
"msize": "29.1GB",
"morigin": "dmhy",
"murl": "https://share.dmhy.org/?keyword=%E9%BB%91%E7%8C%ABtopics/view/521371_Monogatari_Series_10bit_BD1080p_HEVC-x265.html"
}

传统的逐个字段更新

1
2
3
4
5
6
m = magnetInfo.object.get(UID='ADBES682BOEO')
m.name = up_info['mtitle']
m.mobile = up_info['mdate']
m.mail = up_info['magnet']
...
m.save()

一顿操作猛如虎

1
2
m = magnetInfo.object.filter(mtitle='物语系列合集(化物语 伪物语 猫物语(黑) 猫物语(白)..')
m.update(**up_info)

总结

相当的厉害,感觉自己就是傻子,甚至用for循环来写。

然而现在加入了批量多属性更新的操作方式。

而且听说有一个更强大批量更新,可以做到多条数据批量插入的ORM方法,速度比hbase批量存储还要快。

bulk_create()方法!