Sunday, 25 August 2013

Django Tastypie Nested parent / child

Django Tastypie Nested parent / child

I have a parent / child relationship in the same model. Example:
Parent Comment

Child comment 01

Child comment 02
I'd like to build an API that brings all of the child threads in nested
fasion. Currently it just brings up the parent comments.
My current API.py looks like this:
class ThreadResource(ModelResource):
locations = fields.ToManyField('forum.api.comments','parent', full=True)
class Meta:
queryset = comments.objects.all()
resource_name = 'Comments'
class comments(ModelResource):
class Meta:
queryset = comments.objects.all()
resource_name = 'comms'
The way i did that in models is:
class comments(models.Model):
title = models.CharField(max_length=255)
parent = models.ForeignKey('self', blank=True,null=True)
sort = models.IntegerField(default=0)
content = models.CharField(max_length=255)

No comments:

Post a Comment