I have a parent / child relationship in the same model. Example:
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)
First, you need to define a filter function that will return a queryset of the parent comments. Let's call it filter_comments_per_bundle:
def filter_comments_per_bundle(bundle);
parent = bundle.obj
return comments.objects.filter(parent=parent)
Next, just add a reference to self in the comments model resource:
children = fileds.ToManyField('self', filter_comments_per_bundle, full = True, null = True)
Lastly, sorry but it's a pet peeve. s/comments/Comment/ Models should be singular, with first letter capital.
Oh, and another thing. Don't name Models and ModelResources same. Rename comments ModelResource.