假设我有:
模型.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
然后我尝试\"owner\" : \"/api/v1/user/1/\"
通过将其添加到BooksResource
def hydrate_owner(self, bundle):
bundle.obj.owner = User.objects.get(pk = bundle.request.user.id)
return bundle
当我在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
伙计们能帮帮我吗?