Tracking a reverse relationship for a foreignkey in django-reversion
Asked Answered
M

1

5

I'm trying to figure out how how to track changes for a foreignkey relationship in Django using Django-reversion.

In short, I am trying to model a Codelist, which contains Codes which only belong to one Codelist. This can be modelled using a foreign key like so:

class CodeList(models.Model):
    name = models.CharField(max_length=100)

class Code(models.Model):
    value = models.PositiveIntegerField(max_length=100)
    meaning = models.CharField(max_length=100)
    codelist = models.ForeignKey(CodeList,related_name="codes")

Additionally, the only way to edit a code is by using an inline form in the admin site accessed via its codelist. For all intents and purposes, codes belong to codelists as they should...

Except when it comes to reversion.

I'm using the reversion.middleware.RevisionMiddleware to track all editing changes, as there are some non-admin forms for editing codes.

What I'd like is when I see the history of a codelist, it should changes to the codes as well, but I can't figure that out in the Django-reversion API. The issue is that the API covers tracking the code, and seeing changes to the codelist, not the other way around by following the reversed relationship.

Is anyone aware of how this might be done?

Monzonite answered 11/1, 2015 at 22:56 Comment(0)
M
7

Its not well documented Its very well documented, I just couldn't find it, but you can just add the inverse relationship as the field to follow like so:

reversion.register(CodeList, follow=["codes"])
Monzonite answered 11/1, 2015 at 23:9 Comment(1)
Any idea on how to access to information for the FK? versions[1].field_dict.items() will give me all of the fields but it doesn't list the FK field.Fresher

© 2022 - 2024 — McMap. All rights reserved.