Search code examples
pythondjangodjango-modelsdjango-reversion

django reversion - revert related field to the same version


I have two models:

class Plan(models.Model):
    ...

class Sales(models.Model):
    plan = models.ForeignKey(Plan)

I registered these models in django-reversion:

reversion.register(Plan, follow=['sales_set'])
reversion.register(Sales)

So, when I get an old version of Plan with old information in it, I tried to get related objects for this old version of Plan like this:

version.object_version.object.sales_set.all()

But I get the newest versions of these related objects. So, how can I get an old versions of Sales?


Solution

  • I found the way:

    You should revert current revision of object, get related objects and revert the revision again. Like this:

    version.revision.revert()
    sales = version.object.sales_set.all()
    version.revision.revert()