A
A
Antigo_ptz2017-01-11 09:23:24
Django
Antigo_ptz, 2017-01-11 09:23:24

Django Rest will transaction fire in decorator?

Hello! In a Django project, I created a decorator to write the history of changes to a separate table

def change_history(a_function_to_decorate):
    @transaction.atomic
    def wrapper(self, request, *args, **kwargs):
       
        #Код для создания записи в таблице с историей изменения документа

        return a_function_to_decorate(self, request, *args, **kwargs)

    return wrapper

I hang this decorator on the put and update methods in the class-based view
class HospitalDetailView(HospitalMixin, generics.RetrieveUpdateDestroyAPIView):

    @change_history
    def put(self, request, *args, **kwargs):
        return super().put(request, *args, **kwargs)

    @change_history
    def delete(self, request, *args, **kwargs):
        return super().delete(request, *args, **kwargs)

1. Have I put the @transaction.atomic annotation in the decorator correctly (will the transaction work correctly)?
2. Is there any way to put the @change_history decorator annotation without overriding the put and delete methods (due to these overrides, there is a lot of duplicated code in the code, because when working with each system model, a table with a change history is written to) ?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question