Manual commit in Django 1.8

How do you implement @commit_manuallyin Django 1.8?

I am trying to update Django 1.5 code to work with Django 1.8, and for some bizarre reason, the decoder commit_manuallywas removed in Django 1.6 without a direct replacement. My process iterates through thousands of records, so it cannot completely wrap the whole process in a single transaction without running out of memory, but to improve performance, it is still necessary to group some records in a transaction. For this, I had a method wrapped with @commit_manually that called transaction.commit () every N iterations.

I cannot say for sure from the docs , but this is still supported. I just need to call set_autocommit(False)instead of a convenient decorator. Is it correct?

+4
source share
1 answer

Yes, you did it. Call set_autocommit(False)to start a transaction, then call commit()and set_autocommit(True)to commit it.

You can wrap this in your own decorator:

def commit_manually(fn):
    def _commit_manually(*args, **kwargs):
        set_autocommit(False)
        res = fn(*args, **kwargs)
        commit()
        set_autocommit(True)
        return res
    return _commit_manually
+7
source

Source: https://habr.com/ru/post/1584343/


All Articles