Sirius Sirius - 1 year ago 99
Python Question

TypeError while using django rest framework tutorial

I am new to using Django Rest framework, i am following this tutorial

Instead of snippets my model consists of a userprofile as given below:

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)

The first part of the tutorial ran fine, got the desired output in json format as mentioned, however the second tutorial onwards i am getting type error:

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/ in get_permissions, line 226
Python Executable: /usr/bin/python
Python Version: 2.7.3
Python Path:
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/ 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/ in view
return self.dispatch(request, *args, **kwargs) ...
▶ Local vars
/home/web/cptm_venv/lib/python2.7/site-packages/django/views/decorators/ in wrapped_view
return view_func(*args, **kwargs) ...
▶ Local vars
/home/web/cptm_venv/lib/python2.7/site-packages/rest_framework/ in dispatch
response = self.handle_exception(exc) ...
▶ Local vars
/home/web/cptm_venv/lib/python2.7/site-packages/rest_framework/ in dispatch
self.initial(request, *args, **kwargs) ...
▶ Local vars
/home/web/cptm_venv/lib/python2.7/site-packages/rest_framework/ in initial
self.check_permissions(request) ...
▶ Local vars
/home/web/cptm_venv/lib/python2.7/site-packages/rest_framework/ in check_permissions
for permission in self.get_permissions(): ...
▶ Local vars
/home/web/cptm_venv/lib/python2.7/site-packages/rest_framework/ in get_permissions
return [permission() for permission in self.permission_classes] ...
▶ Local vars

The rest of the code is almost same as given in the above link in 2nd part and 3rd part:

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

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)

I am missing something very obvious, tried a lot to search what exactly the "type object not iterable" means in this context, and which object is causing the problem, but no luck. I am using Django Rest Framework version 2.3.

Thanks in advance

Answer Source

As pointed out by Daniel above, i had this stupid snippet in the settings file, which was causing the problem,

#   '''Use hyperlinked styles by default'''
#   '''only used if serializer_class attribute is not set on a view'''
#         'rest_framkework.serializers.HyperLinkedModelSerializer',
#          'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
# }

Commmented this and it worked.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download