首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >django-reversion及相关模型

django-reversion及相关模型
EN

Stack Overflow用户
提问于 2013-10-11 08:35:27
回答 1查看 2.2K关注 0票数 19

我有以下models.py

代码语言:javascript
复制
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的基于类的视图,我可以在创建两个联系人之间的新关系时随时创建修订

代码语言:javascript
复制
# 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创建两个版本?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19308447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档