我是使用Django Rest框架的新手,我正在学习本教程 Django-Rest-Framework
我的模型不是代码片段,而是由下面给出的userprofile组成:
class UserProfile(models.Model):
user = models.OneToOneField(User)
emp_code = models.CharField(max_length=10, blank=True)
user_type = models.IntegerField(max_length=1, default=0, choices=USER_TYPE)
group = models.ForeignKey(Group, null=True, blank=True)
status = models.SmallIntegerField(max_length=1,default=0)
added_on = models.DateTimeField(auto_now_add=True)
本教程的第一部分运行正常,如上所述以json格式获得所需的输出,但是第二个教程以后我得到了类型错误:
TypeError at /authentication/userprofile/
'type' object is not iterable
Request Method: GET
Request URL: http://*****.com/authentication/userprofile/
Django Version: 1.6
Exception Type: TypeError
Exception Value:
'type' object is not iterable
Exception Location: /home/web/cptm_venv/lib/python2.7/site- packages/rest_framework/views.py in get_permissions, line 226
Python Executable: /usr/bin/python
Python Version: 2.7.3
Python Path:
['/home/web/cptm_venv/lib/python2.7/site-packages',
'/home/web/cptm',
'/home/web/cptm_venv/lib/python2.7/site-packages',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/pymodules/python2.7']
Server time: Wed, 11 Dec 2013 17:33:54 +0530
Traceback Switch to copy-and-paste view
/home/web/cptm_venv/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/home/web/cptm_venv/lib/python2.7/site-packages/django/views/generic/base.py in view
return self.dispatch(request, *args, **kwargs) ...
▶ Local vars
/home/web/cptm_venv/lib/python2.7/site-packages/django/views/decorators/csrf.py in wrapped_view
return view_func(*args, **kwargs) ...
▶ Local vars
/home/web/cptm_venv/lib/python2.7/site-packages/rest_framework/views.py in dispatch
response = self.handle_exception(exc) ...
▶ Local vars
/home/web/cptm_venv/lib/python2.7/site-packages/rest_framework/views.py in dispatch
self.initial(request, *args, **kwargs) ...
▶ Local vars
/home/web/cptm_venv/lib/python2.7/site-packages/rest_framework/views.py in initial
self.check_permissions(request) ...
▶ Local vars
/home/web/cptm_venv/lib/python2.7/site-packages/rest_framework/views.py in check_permissions
for permission in self.get_permissions(): ...
▶ Local vars
/home/web/cptm_venv/lib/python2.7/site-packages/rest_framework/views.py in get_permissions
return [permission() for permission in self.permission_classes] ...
▶ Local vars
其余代码与第2部分和第3部分中的上述链接几乎相同: views.py
from apps.authentication.models import UserProfile
from apps.authentication.serializers import UserProfileSerializer
from rest_framework import mixins
from rest_framework import generics
class UserProfileList(mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
class UserProfileDetail(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
urls.py
from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns
from apps.authentication import views
urlpatterns = patterns('',
url(r'^userprofile/$', views.UserProfileList.as_view()),
url(r'^userprofile/(?P<pk>[0-9]+)/$', views.UserProfileDetail.as_view()),
)
urlpatterns = format_suffix_patterns(urlpatterns)
我遗漏了一些非常明显的东西,尝试了很多东西来搜索“不可迭代的类型对象”在这个上下文中意味着什么,以及哪个对象导致问题,但没有运气。我正在使用Django Rest Framework 2.3版。
提前致谢
答案 0 :(得分:104)
只是为了让其他人知道,我一直收到同样的错误,发现我忘记在我的REST_FRAMEWORK
中加入逗号。我有这个:
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated'
),
而不是:
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
逗号将其定义为one-element tuple
答案 1 :(得分:12)
就我而言,拼写错误在views.py
。而不是......
permission_classes = (permissions.IsAuthenticated,)
......我有......
permission_classes = (permissions.IsAuthenticated)
答案 2 :(得分:8)
正如丹尼尔上面指出的那样,我在设置文件中有这个愚蠢的片段,这导致了问题,
#REST_FRAMEWORK = {
# '''Use hyperlinked styles by default'''
# '''only used if serializer_class attribute is not set on a view'''
# 'DEFAULT_MODEL_SERIALIZER_CLASS':
# 'rest_framkework.serializers.HyperLinkedModelSerializer',
# 'DEFAULT_PERMISSION_CLASSES':
# 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
# }
对此进行了评论并且有效。
答案 3 :(得分:6)
当我使用自定义权限时,由于“拼写错误”
,我遇到了同样的错误我有:
@permission_classes(EventByFacilityPermissions)
class EventByFacilityViewSet(viewsets.ModelViewSet):
而不是:
@permission_classes((EventByFacilityPermissions,))
class EventByFacilityViewSet(viewsets.ModelViewSet):
答案 4 :(得分:4)
如果您需要在某些类中进行身份验证,请将认证类传递到您的类而不是您的settings.py中,并执行以下操作:
authentication_classes = [TokenAuthentication, ]
而不是像这样:
authentication_classes = TokenAuthentication