N
N
nurzhannogerbek2017-03-28 16:20:17
Django
nurzhannogerbek, 2017-03-28 16:20:17

Ready-made solutions for version control of the data model?

Hello! Need advice.
There is a certain data model and the task is to store all information about changes (some kind of version control) and, if necessary, roll back to a specific version. I am sure that similar problems have already been solved. Could you recommend projects, ready-made applications or links to articles on this topic.
I myself found two projects on github django-simple-history ( https://github.com/treyhunner/django-simple-history) and django-reversion ( https://github.com/etianen/django-reversion), but The problem is that everything is adapted for display in the admin panel. When I need to display the version history of changes on, let's say, a custom page.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-03-28
@nurzhannogerbek

In django-reversion, changes can be tracked and managed outside of the admin. An example of adding an object to the database along with the initial version:

@require_POST
@login_required
@transaction.atomic()
@reversion.create_revision()
def add_something(request):
    form = SomeModelForm(request.POST)
    if form.is_valid():
        instance = form.save()
        reversion.set_user(request.user)
        reversion.set_comment(u'Что-то добавлено')
        return redirect(instance)
    else:
        return render(request, 'form.html', {'form': form})

Completely similar change and removal.
If you need to get a list of revisions of an object:
versions = Version.objects.get_for_object(instance)
for n, version in enumerate(versions, start=1):
    print(u'В версии №{} от {}'.format(n, version.revision.date_created))
    for field_name, field_value in version.field_dict.items():
        print(u'поле "{}" имело значение "{}"'.format(field_name, field_value))
    print()

And if you need to roll back to any version, for example, to the initial one:
versions = Version.objects.get_for_object(instance)
versions[0].revision.revert()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question