2

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"}]
Community
  • 1
  • 1
Praneeth
  • 902
  • 2
  • 11
  • 25

1 Answers1

1

You just need to setup a variable to hold to previous item and insert it into the dictionary as required. note that this uses getattr as a way to get the object_repr of previous_object and safely provide a default if there is none (as would be the case for the first iteration).

data = []
previous_object = None
for i in history_list:
    data.append({
        'date_time': str(i.revision.date_created),
        'user': str(i.revision.user),
        'object': i.object_repr,
        'previous_object': getattr(previous_object,'object_repr',None),
        'type': i.content_type.name,
        'comment': i.revision.comment
    })
    previous_object = i
  • OUTPUT:- [{"previous_object": null, "comment": "Changed name.", "type": "sometype", "user": "someuser", "object": "newobject", "date_time": "2015-03-03 01:16:00.529507+00:00"}, {"previous_object": "newobject", "comment": "Changed name.", "type": "sometype", "user": "someuser", "object": "someobject", "date_time": "2015-03-03 01:15:20.657896+00:00"}] I would like to get output as example below: for instance: original object:"apple" after i went into admin and changed original object:"orange" it should represent previous object: apple, original object:orange – Praneeth Mar 03 '15 at 01:27
  • Edit that into your question so you can format it properly, its hard to understand. –  Mar 03 '15 at 02:05