刚刚完成官方教程的第5部分,我遇到了一个问题。超链接的API工作得很好,期待我点击一个片段。例如,在以下内容中:
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"url": "http://localhost:8000/snippets/1/",
"owner": "ankush",
"title": "",
"code": "print 123",
"linenos": false,
"language": "python",
"style": "friendly",
"highlight": "http://localhost:8000/snippets/1/highlight/"
}
]
}
点击网址会给我一个例外:' Snippet Serializer'对象不可调用。我以为我从教程中正确复制了一切,但显然我没有。代码在这里:https://github.com/ankush981/rest-demo
Finally, here's the entire trace:
Environment:
Request Method: GET
Request URL: http://localhost:8000/snippets/1/
Django Version: 1.9.7
Python Version: 3.4.3
Installed Applications:
('rest_framework',
'snippets',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "/media/common/code/python/django-rest/tutorial/env/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/media/common/code/python/django-rest/tutorial/env/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/media/common/code/python/django-rest/tutorial/env/lib/python3.4/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/media/common/code/python/django-rest/tutorial/env/lib/python3.4/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/media/common/code/python/django-rest/tutorial/env/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
466. response = self.handle_exception(exc)
File "/media/common/code/python/django-rest/tutorial/env/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
463. response = handler(request, *args, **kwargs)
File "/media/common/code/python/django-rest/tutorial/env/lib/python3.4/site-packages/rest_framework/generics.py" in get
286. return self.retrieve(request, *args, **kwargs)
File "/media/common/code/python/django-rest/tutorial/env/lib/python3.4/site-packages/rest_framework/mixins.py" in retrieve
57. serializer = self.get_serializer(instance)
File "/media/common/code/python/django-rest/tutorial/env/lib/python3.4/site-packages/rest_framework/generics.py" in get_serializer
111. return serializer_class(*args, **kwargs)
Exception Type: TypeError at /snippets/1/
Exception Value: 'SnippetSerializer' object is not callable
答案 0 :(得分:2)
不应该这样:
class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
'''Retrieve, update or delete a snippet'''
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer()
permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly)
那样:
class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
'''Retrieve, update or delete a snippet'''
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly)