4

確かに私はこれを理解するのに苦労しています、

私のローカル環境では、djangoUserオブジェクトのユーザー名ルックアップを統合しました。

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"),
        ]

tastypieのソースであるresources.pyに、1800行目以降に2つのprintステートメントを追加しました。

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オブジェクトを返します。

ただし、リモート(開発)サーバーではまったく同じセットアップを使用しています(すべてのファイルを再確認しました)。唯一の違い、2.6ではなくおいしいpython 2.7 egを実行していることです。とにかく、次のように表示されます。

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の卵の問題のようです。

于 2012-07-27T09:07:08.410 に答える
4

Tastypie 0.10.0は、override_urlsではなくprepend_urlsを使用します

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

于 2014-12-23T20:25:51.727 に答える
2

prepend_urlstastypieバージョン0.9.12a用です。

tastypie v0.9.11では、override_urls代わりに使用する必要があります。(これによると

于 2012-08-28T14:28:31.673 に答える