Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django为基于类的重定向视图设置事务_Python_Django_Transactions - Fatal编程技术网

Python Django为基于类的重定向视图设置事务

Python Django为基于类的重定向视图设置事务,python,django,transactions,Python,Django,Transactions,我有以下基于类的简单切换机制视图: 类PostLikeToggle(重定向视图): 我考虑将此方法作为一个原子事务,但不确定正确的方法。 我走对了吗 class PostLikeToggle(RedirectView): @transaction.atomic def get_redirect_url(self, *args, **kwargs): 有两种方法,首先是你说的: class PostLikeToggle(RedirectView): @transactio

我有以下基于类的简单切换机制视图:

类PostLikeToggle(重定向视图):

我考虑将此方法作为一个原子事务,但不确定正确的方法。 我走对了吗

class PostLikeToggle(RedirectView):
    @transaction.atomic
    def get_redirect_url(self, *args, **kwargs):

有两种方法,首先是你说的:

class PostLikeToggle(RedirectView):
    @transaction.atomic
    def get_redirect_url(self, *args, **kwargs):
第二种选择:

def get_redirect_url(self, *args, **kwargs)
    try: 
        with transaction.atomic(): 
            pass  # CRUD operations 
    except IntegrityError: 
        handle_exception()  # this will run after rollback
def get_redirect_url(self, *args, **kwargs)
    try: 
        with transaction.atomic(): 
            pass  # CRUD operations 
    except IntegrityError: 
        handle_exception()  # this will run after rollback