Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic transactions in DRF action?

I am trying to get into Django atomic transactions at first time. I wonder if there is a possibility to use it something like this:

class TaskViewSet(MultiSerializerViewSet):
    @transaction.atomic
    @action(methods=['PATCH'], detail=True)
    def move_task(self, request, pk):
        # making queries, trying to update them, roll back if last one fails.    
        return Response("message: SUCCESS", status=_status.HTTP_200_OK)

I have searched a bit - there is some information about how to use transactions but I did not find any info if it is possible to use them with DRF.

like image 561
Chiefir Avatar asked Oct 11 '25 20:10

Chiefir


1 Answers

class PayViewSet(ModelViewSet):

    @action(methods=['PATCH'], detail=True)
    @transaction.atomic
    def approval(self, request, *args, **kwargs):
        sid = transaction.savepoint()
        success = something 
        if success:
            transaction.savepoint_commit(sid)
            return success_response('yes')
        else:
            transaction.savepoint_rollback(sid)
            return error_response('no')

savepoint is optionally depending on your situation

like image 152
Ykh Avatar answered Oct 14 '25 18:10

Ykh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!