User Tools

Site Tools


django:django-reversion

DJANGO-REVERSION

Low-level API

Please refer to django-reversion low level API v 1.8

It is valid for lower versions too.

Switch from "django-pro-history" to "django-reversion"

The steps to follow are:

  1. delete fields django-pro-history fields from the models, where they are present;
  2. register the models from the previous step to django-reversion;
  3. create initial revisions for the registered models;
  4. use django-pro-history methods instead of django-reversion methods in “base” models API;
  5. migrate registred models with South to remove django-pro-history fields from the db;

The final results to check are:

  1. it is possible to obtain programmaticly the different versions of an objects;
  2. it is possible to obtain programmaticly the different versions of an objects for a certain time interval;
  3. the models do not have django-pro-history fields anymore and are registered to django-reversion;
  4. django-reversion added to the base requirements;
  5. history fields “eradicated” from db;
  6. it is possible to install GF including django-reversion;
  7. it is possible to upgrade GF with django-reversion through an automatic script.

Delete fields django-pro-history fields from the models, where they are present

“history” fields removed from the models:

  • gas/models/order.py
    • GASSupplierOrder
    • GASSupplierOrderProduct
    • GASMemberOrder
    • Delivery
    • Withdrawal
  • supplier/models.py
    • Supplier
    • SupplierAgent
    • Certification
    • ProductCategory
    • ProductMU
    • ProductPU
    • Product
    • SupplierStock
  • base/models.py
    • Person
    • Contact
    • Place
    • DefaultTransition
  • gas/models/base.py
    • GAS
    • GASConfig
    • GASActivist
    • GASMember
    • GASSupplierStock
    • GASSupplierSolidalPact

Register the models from the previous step to django-reversion

For each of the model in the previous step, add:

import reversion 
.
.
.
class MyModel():
    .
    .
    .
    
if not reversion.is_registered(MyModel):
    reversion.register(MyModel)

Create initial revisions for the registered models

Exec:

django-admin.py createinitialrevisions [app[.model]] [-v {0,1,2,3}]

Use django-pro-history methods instead of django-reversion methods in "base" models API

Migrate registred models with South to remove django-pro-history fields from the db

Django-reversion low level API

It is possible to use django-reversion to retrieve information from the saved objects versions.

To produce versions from an object updates, django-reversion has to be instructed accordingly.

The easiest way is to register the model to the reversion.middleware.RevisionMiddleware.

NOTE: django-reversion version 1.5.7 does not save the User for the incoming requests into the revision.
If you want to have this facility, please use beFair version based on version 1.5.7 .

From then, it is possible to retrieve versions and info from an object.

All object revisions

import reversion

from app.models import mymodel

obj = mymodel.objects.get(pk=1)

reversion.get_for_object(obj)

Object revisions for a time interval

import reversion

from app.models import mymodel

obj = mymodel.objects.get(pk=1)

reversion.get_for_object(obj).filter(revision__date_created__gt=datetime.datetime(y, m, d), revision__date_created__lt=datetime.datetime(y1, m1, d1))
django/django-reversion.txt · Last modified: 2015/04/09 12:44 by letti