Tracking and printing fields that were changed

212 views
Skip to first unread message

anupa...@gmail.com

unread,
May 30, 2017, 8:24:10 AM5/30/17
to django-reversion discussion group
I spent a while searching for a good solution to this but couldnt figure an ideal way yet, so thought to post it here, before deciding on anything.

In the History of a model instance on the admin, I would ideally like to log what fields were edited along with their old and new values. Something like the following:

Edited: 
Name (John -> Sarah)
State (VA -> CA)

This seems to have been discussed a few times on SO and perhaps also on this list. The available options seem to be the following:

1) Fetch the existing model instance before saving the new model instance. Compare each field. Put the changed field in reversion.set_comment('(all changes here)'). Continue with saving the model instance.
2) Save a copy of the old fields separately in model's  __init__() and later in model's save(), compare the new fields with them to track what changed. Put the changed field in reversion.set_comment('(all changes here)'). Continue with saving the model instance. (This approach will save a DB lookup)
3) Generate a diff as mentioned here and integrate with the Admin somehow (honestly, dont know what it'll take to do that as compared to the above approaches)

Would be interesting to know if there is any other way to do the above/which ones of the above seems more intuitive. The last one seems to be utilising django-reversion more than other approaches, since it already has full revisions stored, so I am leaning more towards it, but eager to hear thoughts.

Anupam

Dave Hall

unread,
May 30, 2017, 8:41:15 AM5/30/17
to django-reversion discussion group
It's a tricky problem. Option 3 involves no duplication of data, so I guess that's the best. It also avoids a race condition in revision creation.

--
You received this message because you are subscribed to the Google Groups "django-reversion discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-reversi...@googlegroups.com.
To post to this group, send email to django-r...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-reversion.
For more options, visit https://groups.google.com/d/optout.

Anupam Jain

unread,
May 31, 2017, 12:40:00 AM5/31/17
to django-reversion discussion group
Thanks. For 3, I also found jedie/django-reversion-compare which seems to be working well for this task right now (it doesnt put the changes in the comments, but one more click to see the changes should be ok for now). It maybe using reversion's diff API anyways but saving the trouble of integrating it with Admin ourselves. If it doesnt work for future needs, perhaps I'll try to do something by using the diff API and the reversion.signals.pre_revision_commit to set the diff as a comment for the revision.

On Tue, May 30, 2017 at 6:11 PM, Dave Hall <da...@etianen.com> wrote:
It's a tricky problem. Option 3 involves no duplication of data, so I guess that's the best. It also avoids a race condition in revision creation.

On Tue, 30 May 2017 at 13:24 <anupa...@gmail.com> wrote:
I spent a while searching for a good solution to this but couldnt figure an ideal way yet, so thought to post it here, before deciding on anything.

In the History of a model instance on the admin, I would ideally like to log what fields were edited along with their old and new values. Something like the following:

Edited: 
Name (John -> Sarah)
State (VA -> CA)

This seems to have been discussed a few times on SO and perhaps also on this list. The available options seem to be the following:

1) Fetch the existing model instance before saving the new model instance. Compare each field. Put the changed field in reversion.set_comment('(all changes here)'). Continue with saving the model instance.
2) Save a copy of the old fields separately in model's  __init__() and later in model's save(), compare the new fields with them to track what changed. Put the changed field in reversion.set_comment('(all changes here)'). Continue with saving the model instance. (This approach will save a DB lookup)
3) Generate a diff as mentioned here and integrate with the Admin somehow (honestly, dont know what it'll take to do that as compared to the above approaches)

Would be interesting to know if there is any other way to do the above/which ones of the above seems more intuitive. The last one seems to be utilising django-reversion more than other approaches, since it already has full revisions stored, so I am leaning more towards it, but eager to hear thoughts.

Anupam

--
You received this message because you are subscribed to the Google Groups "django-reversion discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-reversion+unsubscribe@googlegroups.com.
To post to this group, send email to django-reversion@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "django-reversion discussion group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-reversion/_vzvnHsRBFI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-reversion+unsubscribe@googlegroups.com.
To post to this group, send email to django-reversion@googlegroups.com.



--
"What is meditation?" (http://www.heartfulness.org/)
Reply all
Reply to author
Forward
0 new messages