Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define transaction.atomic in a Django class-based view?

Tags:

django

I want the post method in the class-based view to be atomic. I have defined the class so:

class AcceptWith(View):
    @method_decorator(login_required)
    @method_decorator(user_passes_test(my_test))
    @method_decorator(transaction.atomic)
    def dispatch(self, *args, **kwargs):
        return super(AcceptWith, self).dispatch(*args, **kwargs)
  1. Is this correct?
  2. Can I make only the post method atomic?
like image 968
Jesvin Jose Avatar asked Nov 27 '14 14:11

Jesvin Jose


1 Answers

Assuming that you're defining your own method to handle the POST, just apply the transaction.atomic decorator directly to that method.

class AcceptWith(View):
    @transaction.atomic
    def post(self, request, *args, **kwargs):
        # your code here will be executed atomically
like image 96
Kevin Christopher Henry Avatar answered Oct 06 '22 08:10

Kevin Christopher Henry