python - Accessing comments on an object via reverse relationship with Tastypie -
i'm building api using tastypie django , i've run bit of issue.
i have model called moment
(basically blog post, title , body text), , want able attach comments , retrieve them via api. i'm using django.contrib.comments
django 1.6.5 , tastypie 0.11.1.
now, according tastypie documentation, this should straightforward. i've implemented pretty close that. models.py
:
class moment(models.model): """ represents moment - statement user on 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 save() method set 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 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 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, comments come blank.
now, know model seems correct because in django shell, following returns comments on moment:
moment.objects.all()[0].comments.all()
i think problem therefore in api.py
, haven't been able track down. can see i've gone astray?
finally got working 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 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 issue returning moment
object commentresource
, resolved changing attribute content_object
.
Comments
Post a Comment