【问题标题】:django-reversion and related modeldjango-reversion 及相关模型
【发布时间】:2013-10-11 00:35:27
【问题描述】:

我有以下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')

使用基于类的 Django 视图和Revision Context Manager,我可以随时在两个联系人之间创建新关系时创建修订

# 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())

但只有一个联系人获得修订(第一个)和随之而来的评论。如何使用Revision Context Manager 创建两个修订版?

【问题讨论】:

  • 第二次尝试在列表 ['from', 'to'] 中循环。
  • 你是说保存两个联系人?
  • 我知道这个问题很老了,但你有没有解决过这个问题?你是registering the foreign keys to be followed吗?
  • 不幸的是,我们放弃了使用 Reversion 的想法。我们构建了自己的 LogEntry 模型来注册任何更改。事实上,我们真的不需要 Reversion,因为我们的应用程序没有提供恢复操作。但是,是的,注册外键应该可以解决问题。

标签: django django-reversion


【解决方案1】:

可能有点晚了,但我认为使用最新版本的reversion,您可以遵循以下关系:

将此行添加到模型的末尾:

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

【讨论】:

    猜你喜欢
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2012-06-04
    • 2020-10-22
    • 2019-01-18
    • 2022-01-13
    相关资源
    最近更新 更多