django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

'type' object is not iterable

Open pedrotnascimento opened this issue 10 years ago • 3 comments

Checklist

  • [ x] I have verified that that issue exists against the master branch of Django REST framework.
  • [ x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [ x] This is not a usage question. (Those should be directed to the discussion group instead.)
  • [ x] This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • [ x] I have reduced the issue to the simplest possible case.
  • [ x] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

following the django rest tutorial : http://www.django-rest-framework.org/tutorial/5-relationships-and-hyperlinked-apis/#creating-an-endpoint-for-the-highlighted-snippets

it says to add this, to views.py:

class SnippetList(generics.ListCreateAPIView): """ List all snippets, or create a new snipet """ queryset = Snippet.objects.all() serializer_class= SnippetSerializer permission_classes = (permissions.IsAuthenticatedOrReadOnly)

Expected behavior

Work without error

Actual behavior

Give this error in browser(with python 1.8):

TypeError at /snippets/ 'type' object is not iterable Request Method: GET Request URL: http://localhost:8000/snippets/

SOLUTION

change this permission_classes = (permissions.IsAuthenticatedOrReadOnly) for this: permission_classes = (permissions.IsAuthenticatedOrReadOnly,) # note the comma

FURTHER SOLUTION(reason of the oppening issue )

staff check if the behaviour occurs and update the tutorial if so.

pedrotnascimento avatar Nov 19 '16 19:11 pedrotnascimento

You have a typo in your permission class, it should be:

permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

xordoquy avatar Nov 19 '16 19:11 xordoquy

In my case it was not as a list so it was

permission_classes = permissions.IsAuthenticatedOrReadOnly

Instead of

permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

rootux avatar Jul 13 '18 18:07 rootux

@rootux It's not a question of list or no list. Permissions are passed as tuples, same way you define fields in serializer's Meta class. Just add a comma and it will work irrespective of adding brackets

rahul1205 avatar Jul 20 '18 08:07 rahul1205