问题
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 to be able to attach comments to it and retrieve them via the API. I'm using django.contrib.comments
with Django 1.6.5 and Tastypie 0.11.1.
Now, according to the Tastypie documentation, this should be straightforward. What I've implemented is pretty close to that. This is my models.py
:
class Moment(models.Model):
"""
Represents a Moment - a statement by a user on a subject
"""
ZONE_CHOICES = (
('Communication', 'Communication'),
('Direction', 'Direction'),
('Empathy', 'Empathy'),
('Flexibility', 'Flexibility'),
('Motivation', 'Motivation'),
('Ownership', 'Ownership'),
('Persistence', 'Persistence'),
('Reliability', 'Reliability'),
('Teamwork', 'Teamwork'),
)
STATUS_CHOICES = (
('Open', 'Open'),
('More Info', 'More Info'),
('Closed', 'Closed'),
)
title = models.CharField(max_length=200)
text = models.TextField()
datetime = models.DateTimeField(default=timezone.now())
zone = models.CharField(max_length=200,
choices=ZONE_CHOICES)
sender = models.ForeignKey(Student, blank=True, null=True, related_name="sender")
status = models.CharField(max_length=200,
default='Open',
choices=STATUS_CHOICES)
recipient = models.ForeignKey(Sponsor, blank=True, null=True, related_name="recipient")
comments = generic.GenericRelation(Comment, object_id_field='object_pk')
def save(self, *args, **kwargs):
"""
Override the save() method to set the recipient dynamically
"""
if not self.recipient:
self.recipient = self.sender.sponsor
super(Moment, self).save(*args, **kwargs)
def __unicode__(self):
return self.title
class Meta:
ordering = ["-datetime"]
And this is my api.py
:
class MomentResource(BaseResource):
"""
Moment resource
"""
sender = fields.ForeignKey(StudentResource, 'sender', full=True, readonly=True)
comments = fields.ToManyField('myapp.api.CommentResource', 'comments', blank=True, null=True)
class Meta:
"""
Metadata for class
"""
queryset = Moment.objects.all()
resource_name = 'moment'
always_return_data = True
authentication = BasicAuthentication()
authorization = DjangoAuthorization()
filtering = {
'zone': ALL,
}
class CommentResource(ModelResource):
"""
Comment resource
"""
moment = fields.ToOneField(MomentResource, 'moment')
class Meta:
queryset = Comment.objects.all()
resource_name = 'comments'
However, the comments always come back blank.
Now, I know that the model seems to be correct because in the Django shell, the following returns the comments on a Moment:
Moment.objects.all()[0].comments.all()
I think the problem is therefore in api.py
, but I haven't been able to track it down. Can anyone see where I've gone astray?
回答1:
Finally got it working with the following:
class MomentResource(BaseResource):
"""
Moment resource
"""
sender = fields.ForeignKey(StudentResource, 'sender', full=True, readonly=True)
comments = fields.ToManyField('myapp.api.CommentResource', 'comments', null=True, full=True)
class Meta:
"""
Metadata for class
"""
queryset = Moment.objects.all()
resource_name = 'moment'
always_return_data = True
authentication = BasicAuthentication()
authorization = DjangoAuthorization()
filtering = {
'zone': ALL,
}
class CommentResource(BaseResource):
"""
Comment resource
"""
moment = fields.ToOneField(MomentResource, 'content_object')
class Meta:
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
.
来源:https://stackoverflow.com/questions/24453709/accessing-comments-on-an-object-via-reverse-relationship-with-tastypie