问题
I have a Photo application setup using tasty pie. I tried adding functionality to let users like a photo, using a PhotoLike model and resource. I thought it would be a simple addition, but I have been pulling my hair out for two days now :(
I am getting the error below
{
"error_message": "'dict'objecthasnoattribute'obj'",
"traceback": "Traceback(mostrecentcalllast):\n\nFile\"/env/lib/python2.7/site-packages/tastypie/resources.py\",line195,inwrapper\nresponse=callback(request,*args,**kwargs)\n\nFile\"/tastypie/resources.py\",line426,indispatch_list\nreturnself.dispatch('list',request,**kwargs)\n\nFile\"/env/lib/python2.7/site-packages/tastypie/resources.py\",line458,indispatch\nresponse=method(request,**kwargs)\n\nFile\"/env/lib/python2.7/site-packages/tastypie/resources.py\",line1320,inpost_list\nupdated_bundle=self.obj_create(bundle,**self.remove_api_resource_names(kwargs))\n\nFile\"/Users/bilal/Development/GWala/webapp/Guidewala/photowala/api.py\",line172,inobj_create\nreturnsuper(PhotoLikeResource,self).obj_create(bundle,user=bundle.request.user)\n\nFile\"/env/lib/python2.7/site-packages/tastypie/resources.py\",line2083,inobj_create\nbundle=self.full_hydrate(bundle)\n\nFile\"/env/lib/python2.7/site-packages/tastypie/resources.py\",line893,infull_hydrate\nsetattr(bundle.obj,field_object.attribute,value.obj)\n\nAttributeError:'dict'objecthasnoattribute'obj'\n"
}
My model and resource is setup as follow:
class PhotoLike(TimeStamp):
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)
photo = models.ForeignKey(Photo, null=True, blank=True)
class Meta:
unique_together = ("user", "photo")
def __unicode__(self):
return 'like'
And my Resource is setup as follows
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)
Any advice on what I am doing wrong or what the error is referring too would be greatly appreciated!
Thanks for your time and help :)
来源:https://stackoverflow.com/questions/18050847/tastypie-foreign-key-relationship-throwing-error