我在向我的资源发布数据时遇到以下错误,我无法深究这一点。任何帮助将不胜感激。
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
答案 0 :(得分:1)
在tastypie的resources.py中调用此异常:
def obj_get_list(self, bundle, **kwargs):
"""
A ORM-specific implementation of ``obj_get_list``.
Takes an optional ``request`` object, whose ``GET`` dictionary can be
used to narrow the query.
"""
filters = {}
if hasattr(bundle.request, 'GET'):
# Grab a mutable copy.
filters = bundle.request.GET.copy()
# Update with the provided kwargs.
filters.update(kwargs)
applicable_filters = self.build_filters(filters=filters)
try:
objects = self.apply_filters(bundle.request, applicable_filters)
return self.authorized_read_list(objects, bundle)
except ValueError:
因此,您设置的过滤器可能存在问题,或者您可能在网址中执行了错误操作,例如向过滤器添加斜杠,例如/ API / V1 /项目客人/?项目= 33 /