2

将数据发布到我无法深入了解的资源时出现以下错误。任何帮助,将不胜感激。

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
4

1 回答 1

1

这个异常在tastepie的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:

因此,这可能是您设置的过滤器有问题,或者您可能在 url 中做错了什么,例如在过滤器上附加斜杠,例如 /api/v1/project-guests/?project=33/

于 2013-06-12T00:29:12.040 回答