django - Trying to add like functionality to my Photo Resource using Tastypie -
i have website people can photographs. created photoresource , photolikeresource (code below). able add photo, want return number of total likes photo , whether current user likes already. tried returning information in hydrate function (commented out in code, broke api calls). how can accomplish this?
class photoresource(modelresource): tags = fields.manytomanyfield(tagresource, 'tags', null=true, blank=true) primary_image = fields.foreignkey(imageresource, 'primary_image', null=true, blank=true, full=true) user = fields.foreignkey(userresource, 'user') class meta: queryset = photo.objects.all() resource_name = 'photos' authorization = djangoauthorization() allowed_methods = ['get', 'post', 'put'] filtering = { 'primary_image': all, 'featured': } # dehyrdate below breaks photo_like call # def dehydrate(self, bundle): # num_likes = photolike.objects.filter(photo=bundle.obj).count() # bundle.data['num_likes'] = num_likes # user_like = photolike.objects.filter(user=bundle.request.user, photo = bundle.obj).exists() # bundle.data['user_like'] = user_like # return bundle.data class photolikeresource(modelresource): photo = fields.foreignkey(photoresource, 'photo', null=true, blank=true) class meta: queryset = photolike.objects.all() authorization = djangoauthorization() resource_name = 'photo_like' always_return_data = true def obj_create(self, bundle, **kwargs): return super(photolikeresource, self).obj_create(bundle, user=bundle.request.user)
thank time , :)
that should return bundle
, not return bundle.data
.
it's post traceback.
Comments
Post a Comment