My Django website has a photo model representing photos on the system, and I use Django.contrib.commentsit so that users can comment on them. This all works fine, but I would like to expand my Tastypie API to allow access to comments for mine PhotoResourceusing a URL, for example /api/v1/photo/1/comments, where 1 is the photo id. I can make the URL work fine, but no matter what filtering I do, I always return the full set of comments, not just the set for the supplied photo. I have included a cut-out selection of my current API code below:
class CommentResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
queryset = Comment.objects.all()
filtering = {
'user': ALL_WITH_RELATIONS,
}
class PhotoResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
queryset = Photo.objects.all()
filtering = {
'id': 'exact',
'user': ALL_WITH_RELATIONS
}
def prepend_urls(self):
return [url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/comments%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_comments'), name="api_get_comments"),
]
def get_comments(self, request, **kwargs):
try:
obj = self.cached_obj_get(request=request, **self.remove_api_resource_names(kwargs))
except ObjectDoesNotExist:
return HttpGone()
except MultipleObjectsReturned:
return HttpMultipleChoices("More than one resource is found at this URI.")
comment_resource = CommentResource()
return comment_resource.get_list(request, object_pk=obj.id, content_type=ContentType.objects.get_for_model(Photo))
, . , - contrib.comments, ContentTypes, , , , , , Tastypie . , . , - :
ctype = ContentType.objects.get_for_model(obj)
comment_resource = CommentResource()
return comment_resource.get_list(request, object_pk=obj.pk, content_type_id=ctype.id)
.
- , ( )?