M
M
Maxim2014-05-07 11:32:52
Django
Maxim, 2014-05-07 11:32:52

How to save change history in Django model?

I'm trying to include the django-reversion application to save model history . I do according to the documentation and if, save from the administrative interface, then everything works correctly, but no records are made from UpdateView. I'm using Django==1.5.6 and django-reverion==1.7.1.

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.transaction.TransactionMiddleware',
    'reversion.middleware.RevisionMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

Although, if I understood correctly from the documentation , only the following lines should be enough for me:
models.py
# Регистрируем необходимые модели
for report in reports_list():
    reversion.register(report)

views.py
class ReportUpdate(UpdateView):
   ...
    def post(self, request, *args, **kwargs):
        response = super(ReportUpdate, self).post(request, *args, **kwargs)
        self.object = self.get_object()
        self.object.editor = request.user.profile
        self.object.is_filled = True
        with transaction.commit_on_success(), reversion.create_revision():
            self.object.save()
            reversion.set_user(request.user)
            reversion.set_comment('comment...')

        return response

If anyone came across, point out the errors or where to dig, which line to read more carefully ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rostislav Grigoriev, 2014-05-07
@crazyzubr

Try like this:

with reversion.create_revision(manage_manually=True):
    self.object.save()
    reversion.default_revision_manager.save_revision(
            [self.object],
            user=request.user,
            comment='comment...'
    )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question