假設我有:Django的tastypie填充字段
models.py
class Books(models.Model):
owner = models.ForeignKey(User)
title = models.CharField(max_length = 100)
api.py
class UserResource(ModelResource):
#blhblahblah as usual
class BooksResource(ModelResource):
owner= fields.ToOneField(UserResource, 'owner')
class Meta:
queryset = Books.objects.all()
authorizarion = Authorization()
然後,我發出:
curl --dump-header - -H "Content-Type: application/json" -X POST --data "{\"owner\" : \"/api/v1/user/1/\", \"title\" : \"foo\"}" http://localhost:8000/api/data/album/
,它的工作,我得到了我的新Books
然後我嘗試添加該到BooksResource
def hydrate_owner(self, bundle):
bundle.obj.owner = User.objects.get(pk = bundle.request.user.id)
return bundle
當我沒有\"owner\" : \"/api/v1/user/1/\"
再做curl
擺脫\"owner\" : \"/api/v1/user/1/\"
,響應404 not found
然後我嘗試不同的方法:
def obj_create(self, bundle, request = None, **kwargs):
return super(BooksResource, self).obj_create(bundle, request, owner = User.objects.get(pk = request.user.id))
或
def obj_create(self, bundle, request = None, **kwargs):
return super(BooksResource, self).obj_create(bundle, request, owner = User.objects.get(pk = bundle.request.user.id))
我仍然得到404 not found
能傢伙幫我嗎?