I'm using Django-Reversion - is there a way to temporarily re-instate a previous version of an object so that it can be viewed in a DetailView?
Non working code - version.get_object()
would be nice..
class ProductDetailView(DetailView):
def get_object(self):
obj = super(ProductDetailView, self).get_object()
if self.request.GET.get('v') is not None:
version_id = force_text(self.request.GET.get('v'))
version = get_object_or_404(Version, pk=version_id, object_id=force_text(obj.pk))
return version.get_object()
return obj
Copying from my post @ http://spapas.github.io/2015/01/21/django-model-auditing/#using-django-reversion
[...] each version has a
revision
attribute for the corresponding revision and can be used to do the following:revision.user
attributerevision.date_created
attributefield_dict
attributeobject_version.object
attributerevert()
methodSo try changing your
return version.get_object()
line toreturn version.object_version.object
!Update to answer OP's comment for related objects: django-reversion supports revisions that group changes to models -- copying from http://django-reversion.readthedocs.org/en/latest/api.html#creating-revisions
So, if you change all your models in a single POST request you can add it in a revision to make all your changed objects members of that revision. After that, when you have a version of the object you can use its reversion to get all the versions of all objects that belong to the same revision:
version.revision.version_set.all()
.If you don't actually save the related objects when you save the main object so the related objects don't belong to the revision, then yes, you need to go down the rabbit hole and get the revisions of the related objects as they were on the date of the main object revision (this is a rather complex query).