reverse relationships access

258 views
Skip to first unread message

lcplu...@gmail.com

unread,
Aug 3, 2015, 12:08:54 AM8/3/15
to django-reversion discussion group
Hi,

django-reversion is awesome btw.

I have setup django reversion to follow the FK and reverse relations.

class A():
    pass

class B():
    a = models.ForeignKey(A, related_name='b')

reversion.register(A, follow=['b'])
reversion.register(B, follow=['a''])

I only need django reversion to keep track of a before and after state.

a = A()
a.save()

b = B(a=a)
b.save()

now assume I create a revision for A based on its current state, that drills down into B as well, and it works, but please see below:

then I want to get all the reverse relations.

version_list = reversion.get_for_object(A.objects.filter(pk=1).first())
item = version_list[0]
item_obj = item.object_version.object

item_obj.b.all() -> since reversion is setup to follow the reverse relation when saving reversion for A object, why doesn't give me objects in before state?

for this to work, I have to iterate through the objects, and call get_for_object on indiviudal b's, and then do item.object_version.object

is there an better way to do this for reverse relations?

appreciate any response.

lcplu...@gmail.com

unread,
Aug 3, 2015, 12:10:35 AM8/3/15
to django-reversion discussion group, lcplu...@gmail.com
I just want to list the reverse relation side of objects with a given object in the "before" state (which is just the last and only revision).

Dave Hall

unread,
Aug 3, 2015, 5:38:54 AM8/3/15
to django-reversion discussion group, lcplu...@gmail.com
Because you haven't reverted the revision, accessing relations will always retrieve the latest version of the relation from the database. It's unfortunate, but that's how the Django ORM works.

You can actually abuse database transactions to roll back the database to the point of the revision, render your view, then undo the transaction to the "live" version again. During the temporary "rollback" transaction, all relations will work as expected. An example of how to do this is here: https://github.com/etianen/django-reversion/blob/master/src/reversion/admin.py#L179

If there's interest in this approach, I could write a decorator that encapsulates this logic into something reusable and useful.

--
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 http://groups.google.com/group/django-reversion.
For more options, visit https://groups.google.com/d/optout.

lcplu...@gmail.com

unread,
Aug 3, 2015, 10:58:39 AM8/3/15
to django-reversion discussion group, lcplu...@gmail.com
Hi Dave,

Thank you for the response, I will give this a try.
I'm not sure how much interest is in this approach, but I think it would definitely be useful.

lcplu...@gmail.com

unread,
Aug 3, 2015, 11:08:21 AM8/3/15
to django-reversion discussion group, lcplu...@gmail.com
for context, I'm using django reversion in conjunction with DRF, and need to return responses based on querysets of previous versions (before state).


On Monday, August 3, 2015 at 5:38:54 AM UTC-4, Dave Hall wrote:
Reply all
Reply to author
Forward
0 new messages