简体   繁体   English

Django - 批量更新arrayfield行postgres

[英]Django - bulk update arrayfield rows postgres

How do I update multiple rows 如何更新多行

I've a model, 我有一个模特,

class ModelA(models.Model):
    colA = ArrayField(models.IntegerField())

I want to update all the rows of ModelA, ie for eg:- I want to append 1 in all the rows of column colA . 我想更新ModelA的所有行,即例如: - 我想在列colA所有行中追加1

Had it been a normal column, I would have done the following :- 如果这是一个正常的专栏,我会做以下事情: -

ModelA.objects.all().update(colA="foo")

How can I bulk update Arrayfield? 如何批量更新Arrayfield?

I don't think its possible to do currently, I think the best you can do is to do it is to iterate over the models. 我认为目前不可能这样做,我认为你能做的最好的事情就是迭代模型。

for model in ModelA.objects.all():
    for val in model_a.colA:
        val = val + 1
    model.save()

You can do this as an atomic transaction as well if you wish.. Although it may be worth considering if this field really should be an array field. 如果你愿意的话,你也可以将它作为一个原子事务来做。虽然这个字段确实应该是一个数组字段值得考虑。

You can try using the django-bulk-update package. 您可以尝试使用django-bulk-update软件包。 Although I'm not sure whether it can handle ArrayField 虽然我不确定它是否可以处理ArrayField

Why not do a database migration? 为什么不进行数据库迁移? Django's database migration system should allow you to do this easily, I believe. 我相信Django的数据库迁移系统应该可以让你轻松完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM