将数据发布到我无法深入了解的资源时出现以下错误。任何帮助,将不胜感激。
Traceback (most recent call last):
File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 202, in wrapper
response = callback(request, *args, **kwargs)
File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 439, in dispatch_list
return self.dispatch('list', request, **kwargs)
File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 471, in dispatch
response = method(request, **kwargs)
File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 1313, in post_list
updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))
File \"/Users/andrewpryde/Sites/gnats.dev/gNats_site/gNats_site/api/resources.py\", line 132, in obj_create
bundle = super(BlogPostResource, self).obj_create(bundle, **kwargs)
File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 2078, in obj_create
bundle = self.full_hydrate(bundle)
File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 881, in full_hydrate
value = field_object.hydrate(bundle)
File \"/Library/Python/2.7/site-packages/tastypie/fields.py\", line 737, in hydrate
return self.build_related_resource(value, request=bundle.request)
File \"/Library/Python/2.7/site-packages/tastypie/fields.py\", line 653, in build_related_resource
return self.resource_from_uri(self.fk_resource, value, **kwargs)
File \"/Library/Python/2.7/site-packages/tastypie/fields.py\", line 573, in resource_from_uri
obj = fk_resource.get_via_uri(uri, request=request)
File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 810, in get_via_uri
return self.obj_get(bundle=bundle, **self.remove_api_resource_names(kwargs))
File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 2066, in obj_get
raise NotFound(\"Invalid resource lookup data provided (mismatched type).\")
NotFound: Invalid resource lookup data provided (mismatched type).
使用这个 curl 命令
curl -u "a:test" --dump-header - -H "Content-Type: application/json" -X POST --data '{"body": "This will prbbly be my last post.", "pub_date": "2011-05-22T00:46:38", "title": "Another Post"}' http://127.0.0.1:8000/api/v1/posts/
我的资源如下
class BlogPostResource(ModelResource):
"""
used for getting and creating posts
"""
author = fields.ToOneField(CreateUserResource, 'author', null=True, blank=True)
class Meta:
queryset = BlogPost.objects.all()
resource_name = 'posts'
authentication = BasicAuthentication()
authorization = CustomAuthorization()
allowed_methods = ['get', 'post']
always_return_data = True
# def obj_create(self, bundle, **kwargs):
# try:
# print bundle.obj
# # if bundle.obj.author.id != bundle.request.user.id:
# # raise IntegrityError('Cannot add a post for another user')
# bundle = super(BlogPostResource, self).obj_create(bundle, **kwargs)
# except IntegrityError as detail:
# raise BadRequest(detail)
# return bundle
def hydrate(self, bundle):
"""
Adds author field from user as only allowed to create own posts
"""
bundle.data['author'] = '/api/v1/user/%d/' % int(bundle.request.user.id)
return bundle