Refer to the manual for details of how to install and use django-reversion. It includes a list of which version of django-reversion is compatible with each version of Django - worth keeping an eye on as the recommended version for the latest version of django does change.
on production systems we create a shell script to run django administration commands where this document refers to django-admin you should use the appropriate script on the production system.
From django-reversion version 1.10 the import signature changed from “import reversion” to
from reversion import revisions as reversion
Add reversion to a model¶
After the model definition simply include a line to register it e.g. to define
a model called
class ModelWithReversion(models.Model): field1 = CharField(max_lenght=100) class Meta: verbose_name = "Model with Reversion" reversion.register(ModelWithReversion)
If the reversion is added after a model it was initially created you can create initial revision using the management command
django-admin createinitialrevisions your_app.YourModel --comment="Initial revision."
Count of Revision records¶
We’ve added a management command to the base app with list the number of revision records for each model in a project.
to use this command, you must be using
django-reversion version 2.0.5 or
To use the command simply change to the project directory and type:
Space used in the database¶
The space used by postgresql can be monitored using the command (the reversion
tables are prefixed by
psql -U postgres <database name> -c "select relname, relpages from pg_class order by relpages desc;" | grep "reversion_"jk
A more detailed breakdown can be obtained using
psql -U postgres <database name> -c "SELECT *, pg_size_pretty(total_bytes) AS total , pg_size_pretty(index_bytes) AS INDEX , pg_size_pretty(toast_bytes) AS toast , pg_size_pretty(table_bytes) AS TABLE FROM ( SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM ( SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME , c.reltuples AS row_estimate , pg_total_relation_size(c.oid) AS total_bytes , pg_indexes_size(c.oid) AS index_bytes , pg_total_relation_size(reltoastrelid) AS toast_bytes FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE relkind = 'r' ) a ) a order by total_bytes desc limit 50;"
Size of reversion tables in backup¶
To find out how much many lines of the postgresql dump file is given over to reversion:
grep -n "^COPY" <backup file name> > /tmp/bkup-lines.txt
then view the file and search for
reversion_ the difference between the line
number of this copy command and the next one gives the number of lines that the
backup of that table spans.
Reversion in not always appropriate¶
Where a table changes frequently (e.g. for recording stats) it can create a large number of reversion records that serve no useful purpose.
See the next section for how to remove reversion from an existing model.
Remove reversion from an existing model¶
Before removing an existing model from reversion you should remove the existing revision data otherwise this data will be retained but will not be accessable.
To delete existing revision information use the
command as follows:
django-admin deleterevisions <app name>.<Model name>
If there are a large number of records it is better to delete these in chunks
so that we do not create large transaction on which are time consuming on
postgresql e.g. the following script deletes the revision data for a model
Model in the app
app, a day at a time
day=365 while [ $day -gt 0 ]; do day=`expr $day - 1` echo "Deleting reversion older than $day days" django-admin deleterevisions app.Model --days=$day done
This will split the task into smaller chunks.
Once the data is deleted you can then remove the
from the models module.
If you want to delete all revisions, run the
management command without the model parameter.