In my project some users can make changes in one model and I want to
use django-reversion to audit trail some fields (not all) in this
model. Some another fields in this model can be changed by signals and
this fields is not valuable for me - its need to improve admin area
and make my project run more faster. I try to use django-reversion and
see that it save model data after each its post_save signal. if
unmonitored fields is changed, I expect that model data revision is
not created.
Please look at example:
# ~model.py~ ##################################################
from django.db import models
import reversion
class Author(models.Model):
full_name = models.CharField(max_length=65)
articles = models.PositiveSmallIntegerField(blank=True, default=0)
def __unicode__(self):
return self.full_name
class Article(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
author = models.ForeignKey(Author)
if not reversion.is_registered(Author):
reversion.register(Author, fields=['pk', 'full_name'])
def save_article(sender, instance, created, **kwargs):
if created:
a = instance.author
a.articles += 1
a.save()
def delete_article(sender, instance, **kwargs):
a = instance.author
a.articles -= 1
a.save()
models.signals.post_save.connect(save_article, Article,
dispatch_uid="article.post_save")
models.signals.post_delete.connect(delete_article, Article,
dispatch_uid="article.post_delete")
###########################################################
#~tests.py~
__test__ = {"doctest": """
>>> from models import Author, Article
>>> import reversion
>>> from reversion.models import Version
Creating object - 1st rev.
>>> with reversion.revision:
... a = Author.objects.create(full_name='John Dou')
...
>>> Version.objects.get_for_object(a)
[<Version: John Dou>]
Change object - 2nd rev.
>>> with reversion.revision:
... a.title = 'John Smith'
... a.save()
>>> a = Author.objects.get(pk=a.id)
>>> versions = Version.objects.get_for_object(a)
>>> versions
[<Version: John Dou>, <Version: John Smith>]
>>> versions[0].field_dict
{'id': 1, 'full_name': u'John Dou'}
>>> versions[1].field_dict
{'id': 1, 'full_name': u'John Smith'}
Change unwatched field via signal - 2nd rev. still
>>> a.articles
0
>>> with reversion.revision:
... art = Article.objects.create(title="1st article", body="1st
article body", author=a)
...
>>> a.articles
1
>>> a = Author.objects.get(pk=a.id)
>>> versions = Version.objects.get_for_object(a)
>>> versions
[<Version: John Dou>, <Version: John Smith>]
"""}
###############################################################
# ~ tests.log~
======================================================================
FAIL: Doctest: unwatched_fields.tests.__test__.doctest
----------------------------------------------------------------------
Failed example:
versions
Expected:
[<Version: John Dou>, <Version: John Smith>]
Got:
[<Version: John Dou>, <Version: John Dou>]
----------------------------------------------------------------------
Failed example:
versions[0].field_dict
Expected:
{'id': 1, 'full_name': u'John Dou'}
Got:
{'articles': 0, 'id': 1, 'full_name': u'John Dou'}
----------------------------------------------------------------------
Failed example:
versions[1].field_dict
Expected:
{'id': 1, 'full_name': u'John Smith'}
Got:
{'articles': 0, 'id': 1, 'full_name': u'John Dou'}
----------------------------------------------------------------------
Failed example:
versions
Expected:
[<Version: John Dou>, <Version: John Smith>]
Got:
[<Version: John Dou>, <Version: John Dou>, <Version: John Dou>]
----------------------------------------------------------------------
Ran 1 test in 0.052s
FAILED (failures=1)
#########################################################
It was tested with with django 1.1.1 and trunk rev.11778, django-
reversion 1.2 and
trunk rev. 263 from http://django-reversion.googlecode.com/svn/trunk/src
I think I don't understand smth. (
--
You received this message because you are subscribed to the Google Groups "Django Reversion Discussion" group.
To post to this group, send an email to django-r...@googlegroups.com.
To unsubscribe from this group, send email to django-reversi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-reversion?hl=en-GB.