2013-10-11 10 views
18

मैं निम्नलिखित models.pyDjango-प्रत्यावर्तन और संबंधित मॉडल

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 का उपयोग करना, मैं संशोधन किसी भी समय बना सकते हैं मैं 2 संपर्कों के बीच एक नया रिश्ता बनाने की है

# 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 का उपयोग कैसे किया जा सकता है?

+0

अंदर पाश की कोशिश करें एक सूची ['से', 'से'] दूसरी बार व्यस्त रिश्ते के साथ। – jcs

+0

आप दोनों संपर्कों को सहेजने के बारे में बात कर रहे हैं? – TonyEight

+1

मुझे पता है कि यह मुद्दा पुराना है, लेकिन क्या आपने कभी इसे हल किया है? क्या आप [विदेशी कुंजी का पालन करने के लिए पंजीकरण कर रहे हैं] (http://django-reversion.readthedocs.org/en/latest/api.html#advanced-model-registration)? – tutuDajuju

उत्तर

5

शायद थोड़ी देर हो चुकी है, लेकिन मैं प्रत्यावर्तन का नवीनतम संस्करण के साथ लगता है कि आप संबंधों का पालन कर सकते हैं:

अपने मॉडल के अंत करने के लिए इस लाइन में जोड़ें:

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

-1

Django Model Utils मॉडल और मॉडल फ़ील्ड में बदलावों को ट्रैक करने के साथ-साथ कई अन्य वास्तव में अच्छी चीजें करने के लिए नया सबसे अच्छा अभ्यास तरीका है।

+0

यदि आपने [फील्डट्रैकर] (https://django-model-utils.readthedocs.org/en/latest/utilities.html#field-tracker) को संदर्भित किया है तो: ब्रावो! बस हमें फिर क्या चाहिए! Thanx, वैसे भी :) – TonyEight

+0

यह वास्तव में था! हंडी :) – TimmyGee

+0

यह रिवर्सन के बारे में इस प्रश्न का उत्तर नहीं है। – acidjunk

संबंधित मुद्दे