Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tastypie Reverse Relation

I am trying to get my api to give me the reverse relationship data with tastypie.

I have two models, DocumentContainer, and DocumentEvent, they are related as:

DocumentContainer has many DocumentEvents

Here's my 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']

When I hit my api url, I get the following error:

DocumentContainer' object has no attribute 'pod_events

Can anyone help?

Thanks.

like image 399
rookieRailer Avatar asked Nov 19 '12 21:11

rookieRailer


1 Answers

I made a blog entry about this here: http://djangoandlove.blogspot.com/2012/11/tastypie-following-reverse-relationship.html.

Here is the basic formula:

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

It requires

  1. a models.ForeignKey relationship from the child to the parent in this case
  2. the use of a related_name
  3. the top resource definition to use the related_name as the attribute.
like image 65
Victor 'Chris' Cabral Avatar answered Sep 20 '22 11:09

Victor 'Chris' Cabral