【问题标题】:Django TastyPie NotFound("Invalid resource lookup data provided (mismatched type).")Django TastyPie NotFound(“提供的资源查找数据无效(类型不匹配)。”)
【发布时间】:2013-04-28 16:07:40
【问题描述】:

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

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

【问题讨论】:

    标签: django tastypie


    【解决方案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/

    【讨论】:

    • 在可能的情况下,我在过滤器上附加了一个斜杠
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    相关资源
    最近更新 更多