django-reversion and related model
Asked Answered
D

1

21

I have the following models.py

class Contact(models.Model):
    pass

class Link(models.Model):
    pass

class Relationship(models.Model):
    # Documentation
    __doc__ = _(u'Stores a relationship between 2 contacts.')
    # Attributes
    from_contact = models.ForeignKey(Contact, related_name='from_contacts', verbose_name=_(u'first contact'), help_text=_(u'The first contact implicated by the relationship.'))
    link = models.ForeignKey(Link, related_name=u'relationships', verbose_name=_(u'relationship link'), help_text=_(u'The relationship link between both contacts.'))
    to_contact = models.ForeignKey(Contact, related_name='to_contacts', verbose_name=_(u'second contact'), help_text=_(u'The second contact implicated by the relationship.'))

    # Meta-data
    class Meta:
        verbose_name = u'Relationship'
        verbose_name_plural = u'Relationships'
        unique_together = ('from_contact', 'link', 'to_contact')

Using class-based view of Django and the Revision Context Manager, I can create revision anytime I create a new relationship between 2 contacts

# part of my views.py
class RelationshipCreateView(LoginRequiredMixin, CreateView):
    template_name = u'frontend/relationships/relationship-create.html'
    model = Relationship
    form_class = RelationshipForm

    def get_success_url(self):
        return reverse_lazy('contact-detail', kwargs={'contact_pk': self.kwargs['contact_pk']})

    def form_valid(self, form):
        contact = Contact.objects.get(pk=self.kwargs['contact_pk'])
        link = form.cleaned_data['link']
        other = form.cleaned_data['other']
        inverse = form.cleaned_data['inverse_relationship']
        relationship = None
        if not inverse:
            relationship = Relationship(
                from_contact=contact,
                link=link,
                to_contact=other
            )
        else:
            relationship = Relationship(
                from_contact=other,
                link=link,
                to_contact=contact
            )
        with reversion.create_revision():
            relationship.save()
            reversion.set_comment(_(u'A relationship has been added between %(from)s and %(to)s' % {'from': relationship.from_contact, 'to': relationship.to_contact}))
        return HttpResponseRedirect(self.get_success_url())

But only one of the contacts gets the revision (the first) and the comment coming with it. How Revision Context Manager can be used to create both revisions ?

Dulla answered 11/10, 2013 at 0:35 Comment(4)
Try to loop inside a list ['from', 'to'] with inverse relationship the second time.Insalivate
You're talking about saving both contacts ?Dulla
I know this issue is old, but did you ever resolve this? Are you registering the foreign keys to be followed?Nickeliferous
Unfortunately, we dropped the idea of using Reversion. We built our own LogEntry model to register any change. In fact we really didn't need Reversion since no restore action is provided by our app. But yes, registering foreign keys should solve the day.Dulla
L
9

Probably a bit late, but I think with a recent version of reversion you can follow the relations:

Add this line to the end of your model:

reversion.register(Relationship, follow=['from_contact', 'link', 'to_contact'])

Lazar answered 13/11, 2015 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.