In How to add django-reversion to an app developed using django and django-rest framework I have added the below function to get history of objects
from django.http import HttpResponse
from reversion.models import Version
import json
def history_list(request):
history_list = Version.objects.all().order_by('-revision__date_created')
data = []
for i in history_list:
data.append({
'date_time': str(i.revision.date_created),
'user': str(i.revision.user),
'object': i.object_repr,
'type': i.content_type.name,
'comment': i.revision.comment
})
data_ser = json.dumps(data)
return HttpResponse(data_ser, content_type="application/json")
In the urls.py of How to add django-reversion to an app developed using django and django-rest framework I have added a route to history.
When I visit 127.0.0.1:8000/history I get the json data as
[{"object": "someobject", "user": "someuseruser", "type": "sometype", "comment": "Changed name.", "date_time": "2015-03-02 18:04:58.368650+00:00"}]
execution flow: When I visit 127.0.0.1:8000/admin and change the value of above object to "otherobject". when I refresh 127.0.0.1:8000/history. I get one more json field
[{"object": "otherobject", "user": "someuseruser", "type": "sometype", "comment": "Changed name.", "date_time": "2015-03-02 18:04:58.368650+00:00"}]
below is the area I got stuck to add one more additional field to history function:
I would like to include one more field to above iteration in history function. like previous_object: " " to get the object name before its changed even after changing name. for instance:- from 127.0.0.1:8000/admin I changed object name from "apple" to "orange".
When I visit the history route 127.0.0.1:8000/history
[{"object": "apple", "object_before_changed": ""null": "someuseruser", "type": "sometype", "comment": "Changed name.", "date_time": "2015-03-02 18:00:58.368650+00:00"}]
I should be able to see as below
[{"object": "orange", "object before_changed": ""apple": "someuseruser", "type": "sometype", "comment": "Changed name.", "date_time": "2015-03-02 18:04:58.368650+00:00"}]