大量模型对象上的Django 1.6事务管理 [英] Django 1.6 Transaction Management on large list of Model Objects

查看:44
本文介绍了大量模型对象上的Django 1.6事务管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短的问题很长:如果我需要遍历模型对象列表,并将其单独保存,则不确定如何解决此问题.

Long question short : If I would need to loop through list of Model Objects, and save them individually, I am unsure how to go about that.

要正确理解这一点,

假设我的模型名称= Foo

Assuming, my Model Name = Foo

示例:

Model Object :
f1 = Foo 1 {
name : foo,
alias : Fooo 1,
description : Fooo 1
}

f2 = Foo 2 {
name : foo_2,
alias : Fooo,
description : Fooo 2
}

f3 = Foo 3 {
name : foo_3,
alias : Fooo 3,
description : Fooo
}

现在我想做的是更改:
名称
(Foo 2)的别名
说明
然后手动执行通过循环更新(保存)每个对象的事务.

Now what I would like to do is change :
name for (Foo 1)
alias for (Foo 2)
description for (Foo 3)
and then manually do the transaction of updating (saving) each object via looping.

foo_list = list()

f1.name = 'foo_1'
foo_list.append(f1)

f2.alias = 'Foo 2'
foo_list.append(f2)

f3.description = 'Fooo 3'
foo_list.append(f3)
@task()
@transaction.atomic()
def bulk_update_decorated(bulky):
    """

    :param bulky: model object list
    :return: True
    """
    sid = None
    try:

        for update_this in bulky:
            update_this.save()            # transaction is having an element
            sid = transaction.savepoint()
        return True
    except SoftTimeLimitExceeded:
        transaction.savepoint_commit(sid) # on loop exit commit

@task()
def bulk_update_internal(bulky):
    """

    :param bulky: model object list
    :return: True
    """
    sid = None
    try:
        with transaction.atomic():
            for update_this in bulky:
                update_this.save()
                sid = transaction.savepoint()
    except SoftTimeLimitExceeded:
        transaction.savepoint_commit(sid) # on loop exit commit

以下是首选的方法(实际上可能可行)?

Which of the following is the preferred way to go (actually which might work) ?

bulk_update_internal(foo_list)

OR

bulk_update_decorated(foo_list)

...............................................

................................................

我要保存/更新大量数据.

I am having quite a large set of data which I want to save/update.

之前我正在遍历并保存它们,但没有似乎是个好主意.

Earlier I was looping through and saving them, which did not seem like a good idea.

不能使用update(),因为我希望每个数据点都是存储不同的信息并单独保存.

Can't use update(), because I want each data point to be storing different information and saved separately.

预期用途

我打算做的是通过使用CELERY,更新模型对象列表.所以我像这样遍历列表

What I intended to do was, via using CELERY, update the list of model objects . So I was looping over the list like

for update_this in bulky: update_this.save()

这意味着将为所有对象调用 save 方法,并且由于Django 1.6是自动提交的,因此这意味着对象将以循环方式保存在数据库中.坏主意.

This would mean save method be called for all the objects, and since Django 1.6 is autocommit, this would mean objects are saved in database in a loop. Bad Idea.

那么,使用transaction.atomic()会在这里帮助我吗?
它将以 autocommit = 0 开头,并在 loop exit 上将 commit 提交到数据库?

So, using transaction.atomic() would help me here right ?
It would start with autocommit = 0 and on loop exit would commit to database ?

对于芹菜,我想使用 sid 在达到软时间限制之前容纳物体.这会是个好策略吗?

for celery i wanted to use sid to hold objects before soft time limit would be reached. This would be a good strategy ?

推荐答案

使用 bulk_update_decorated 版本.但是据我了解,您不需要对保存点进行任何操作.只需从函数中返回,整个事务就会被提交.

Use the bulk_update_decorated version. But as far as I understand you don't need to do something with savepoints. Just return from the function and whole transaction will be committed.

@transaction.atomic
def bulk_update_decorated(bulky):
    try:
        for update_this in bulky:
            update_this.save()
    except SoftTimeLimitExceeded:
        pass

保存点用于部分回滚事务.您无需进行任何回滚,因此在这里使用savepoint毫无意义.

Savepoints are used to partial rollback of transaction. You don't do any rollbacks so using of savepoint makes no sense here.

这篇关于大量模型对象上的Django 1.6事务管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆