2012-11-19 14 views
7

Ich versuche, meine API zu erhalten, um mir die umgekehrten Beziehungsdaten mit tastypie zu geben.Tastypie Reverse Relation

Ich habe zwei Modelle, DocumentContainer und Document, sind sie miteinander verwandt als:

DocumentContainer viele DocumentEvents

hat hier ist mein Code:

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'] 

Wenn ich meine api url getroffen, ich den folgenden Fehler erhalten:

DocumentContainer' object has no attribute 'pod_events 

Kann jemand helfen?

Danke.

Antwort

1

Ihre Linie in class DocumentContainerResource(...) ändern, von

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

zu

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

Das Suffix für pod_event in diesem Fall _set sein soll, aber je nach Situation könnte das Suffix eine der sein folgende:

  • _set
  • _s
  • (kein Suffix)

Wenn jedes Ereignis kann nur bis zu einem Behälter in Verbindung gebracht werden, sollten Sie auch zu ändern:

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

zu:

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

hmm, auch nach den Änderungen funktionierte es nicht für mich. Jetzt heißt es: "Das Objekt 'DocumentContainer' hat kein Attribut 'pod_event_set'" – rookieRailer

+0

@rookieRailer Würden Sie die zugehörigen Snippets aus Ihrem models.py posten? –

+1

ForeignKey ist ein Alias ​​für ToOneField. –

12

Ich habe hier einen Blogeintrag gemacht: http://djangoandlove.blogspot.com/2012/11/tastypie-following-reverse-relationship.html. Hier

ist die Grundformel:

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") 

Es erfordert

  1. eine models.ForeignKey Beziehung von dem Kind an den Eltern in diesem Fall
  2. die Verwendung eines verwandten Namens
  3. die oberste Ressourcendefinition, die related_name als Attribut verwendet.
+0

Danke, das hat mir sehr geholfen. Obwohl ich das Attribut related_name = '[top]' in ToManyField vermisse. Dies stellt sicher, dass Daten in create abgelegt werden. Siehe: http://django-tastypie.readthedocs.org/en/latest/fields.html#tastypie.fields.RelatedField.related_name –