Skip to content Skip to sidebar Skip to footer

Accessing Comments On An Object Via Reverse Relationship With Tastypie

I'm building an API using Tastypie with Django and I've run into a bit of an issue. I have a model called a Moment (basically a blog post, with a title and body text), and I want t

Solution 1:

Finally got it working with the following:

classMomentResource(BaseResource):
    """ 
    Moment resource
    """
    sender = fields.ForeignKey(StudentResource, 'sender', full=True, readonly=True)
    comments = fields.ToManyField('myapp.api.CommentResource', 'comments', null=True, full=True)

    classMeta:
        """
        Metadata for class
        """
        queryset = Moment.objects.all()
        resource_name = 'moment'
        always_return_data = True
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()
        filtering = { 
            'zone': ALL,
        }


classCommentResource(BaseResource):
    """ 
    Comment resource
    """
    moment = fields.ToOneField(MomentResource, 'content_object')

    classMeta:
        queryset = Comment.objects.all()
        resource_name = 'comments'

I'm pretty sure the issue was with returning the Moment object from CommentResource, which was resolved by changing the attribute to content_object.

Post a Comment for "Accessing Comments On An Object Via Reverse Relationship With Tastypie"