4

是的,我很难理解这一点,

在我的本地环境中,我已经为 django User 对象集成了用户名查找:

class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        excludes = ['password', 'email', 'is_staff', 'is_active', 'is_superuser']
        resource_name = 'users'
        include_resource_uri = False
        filtering = {
            'username': ALL
        }

    def prepend_urls(self):
        return [
            url(r"^(?P<resource_name>%s)/(?P<username>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
        ]

在tastepie的源代码resources.py中,我在第1800行添加了两个打印语句

def obj_get(self, request=None, **kwargs):
    """
    A ORM-specific implementation of ``obj_get``.

    Takes optional ``kwargs``, which are used to narrow the query to find
    the instance.
    """
    try:
        print "1, ", kwargs
        base_object_list = self.get_object_list(request).filter(**kwargs)
        print "2, ", base_object_list
        # etcetera

参观:

/api/v1/users/foo/?format=json

这打印:

1,  {'username': foo'}
2,  [<User: foo>]
1,  {'id': 1}
2,  [<User: foo>]

并返回正确的 JSON 对象。

但是,在我的远程(开发)服务器上,我有完全相同的设置(我仔细检查了所有文件),唯一的区别似乎是它运行的是 sweetpie python 2.7 egg 而不是 2.6,无论如何我得到这个打印:

1,  {'pk': u'foo'}
[27/Jul/2012 10:48:37] "GET /api/v1/users/foo/?format=json HTTP/1.0" 404 1219

我也得到了这个堆栈跟踪:

{
error_message: "Invalid resource lookup data provided (mismatched type).",
traceback: "Traceback (most recent call last):

  File "...path.../resources.py", line 192, in wrapper
    response = callback(request, *args, **kwargs)

  File "...path.../resources.py", line 406, in dispatch_detail
    return self.dispatch('detail', request, **kwargs)

  File "...path.../resources.py", line 427, in dispatch
    response = method(request, **kwargs)

  File "...path.../resources.py", line 1051, in get_detail
    obj = self.cached_obj_get(request=request, **self.remove_api_resource_names(kwargs))

  File "...path.../resources.py", line 921, in cached_obj_get
    bundle = self.obj_get(request=request, **kwargs)

  File "/...path.../resources.py", line 1765, in obj_get
    raise NotFound("Invalid resource lookup data provided (mismatched type).")

NotFound: Invalid resource lookup data provided (mismatched type).
"

}

有什么想法吗?

4

3 回答 3

6

对了,答案:

将 prepend_urls 更改为(将被弃用) override_urls 修复了该错误,我将在 github 上报告这个,似乎是 2.7 egg 问题

于 2012-07-27T09:07:08.410 回答
4

Tastypie 0.10.0 使用 prepend_urls 而不是 override_urls

http://django-tastypie.readthedocs.org/en/latest/resources.html?highlight=prepend_urls#Resource.override_urls

于 2014-12-23T20:25:51.727 回答
2

prepend_urls适用于 0.9.12a 版的美味派。

在 sweetpie v0.9.11 中,您应该override_urls改用。(据此

于 2012-08-28T14:28:31.673 回答