2012-11-19 15 views
7

मैं अपने एपीआई को टस्टपी के साथ रिवर्स रिलेशनशिप डेटा देने की कोशिश कर रहा हूं।टेस्टपी रिवर्स रिलेशन

DocumentContainer कई DocumentEvents

यहाँ है मेरे कोड है:

मैं दो मॉडल, DocumentContainer के रूप में है, और DocumentEvent, वे संबंधित हैं

class DocumentContainerResource(ModelResource): 
    pod_events = fields.ToManyField('portal.api.resources.DocumentEventResource', 'pod_events') 
    class Meta: 
     queryset = DocumentContainer.objects.all() 
     resource_name = 'pod' 
     authorization = Authorization() 
     allowed_methods = ['get'] 

    def dehydrate_doc(self, bundle): 
     return bundle.data['doc'] or '' 

class DocumentEventResource(ModelResource): 

    pod = fields.ForeignKey(DocumentContainerResource, 'pod') 
    class Meta: 
     queryset = DocumentEvent.objects.all() 
     resource_name = 'pod_event' 
     allowed_methods = ['get'] 

जब मैं अपने API URL मारा, मैं निम्न त्रुटि प्राप्त करें:

DocumentContainer' object has no attribute 'pod_events 

क्या कोई मदद कर सकता है?

धन्यवाद।

उत्तर

1

अपनी लाइन class DocumentContainerResource(...) में,

pod_events = fields.ToManyField('portal.api.resources.DocumentEventResource', 
           'pod_events') 

से

pod_events = fields.ToManyField('portal.api.resources.DocumentEventResource', 
           'pod_event_set') 

के लिए परिवर्तित करें इस मामले में pod_event के लिए प्रत्यय _set होना चाहिए, लेकिन स्थिति के आधार पर, प्रत्यय में से एक हो सकता है निम्नलिखित:

  • _set
  • _s
  • (कोई प्रत्यय)

प्रत्येक घटना के केवल एक कंटेनर के लिए ऊपर के साथ जुड़ा हो सकता है, तो भी बदलने पर विचार:

pod = fields.ForeignKey(DocumentContainerResource, 'pod') 

रहे हैं:

pod = fields.ToOneField(DocumentContainerResource, 'pod') 
+0

हम्म, परिवर्तन के बाद भी, यह मेरे लिए काम नहीं करता था। अब यह कहता है कि "DocumentContainer 'ऑब्जेक्ट में कोई विशेषता नहीं है' pod_event_set '" – rookieRailer

+0

@rookieRailer क्या आप अपने models.py से संबंधित स्निपेट पोस्ट करना चाहते हैं? –

+1

विदेशीकी ToOneField के लिए एक उपनाम है। –

12

मैंने यहां इस बारे में एक ब्लॉग प्रविष्टि बनाई: http://djangoandlove.blogspot.com/2012/11/tastypie-following-reverse-relationship.html

यहाँ मूलभूत फार्मूला हैः

API.py

class [top]Resource(ModelResource): 
    [bottom]s = fields.ToManyField([bottom]Resource, '[bottom]s', full=True, null=True) 
    class Meta: 
     queryset = [top].objects.all() 

class [bottom]Resource(ModelResource): 
    class Meta: 
     queryset = [bottom].objects.all() 

Models.py

class [top](models.Model): 
    pass 

class [bottom](models.Model): 
    [top] = models.ForeignKey([top],related_name="[bottom]s") 

यह बच्चे से

  1. एक models.ForeignKey संबंध की आवश्यकता है इस मामले में माता-पिता को
  2. संबंधित_नाम
  3. विशेषता के रूप में संबंधित_नाम का उपयोग करने के लिए शीर्ष संसाधन परिभाषा का उपयोग।
+0

धन्यवाद, इससे मुझे बहुत मदद मिली। हालांकि मुझे ToManyField में संबंधित_नाम = '[शीर्ष]' विशेषता याद आती है। यह सुनिश्चित करता है कि डेटा बनाने पर जनसंख्या आ गई है। देखें: http://django-tastypie.readthedocs.org/en/latest/fields.html#tastypie.fields.RelatedField.related_name –

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