TypeError while using django rest framework tutorial

前端 未结 5 843
无人共我
无人共我 2020-12-30 22:19

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

Instead of snippets my model consists of a userprofile as given belo

相关标签:
5条回答
  • 2020-12-30 22:55

    In my case the typo was in views.py. Instead of ...

    permission_classes = (permissions.IsAuthenticated,)
    

    ... I had ...

    permission_classes = (permissions.IsAuthenticated)
    
    0 讨论(0)
  • 2020-12-30 23:04

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

    #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'
    # }
    

    Commmented this and it worked.

    0 讨论(0)
  • 2020-12-30 23:06

    Pass the authentication class inside your class instead of in your settings.py if you need authentication just in some classes, and do like this:

    authentication_classes = [TokenAuthentication, ]
    

    instead of like this:

    authentication_classes = TokenAuthentication
    
    0 讨论(0)
  • 2020-12-30 23:10

    I had the same error when I used custom permissions, because of a 'typo'

    I had:

    @permission_classes(EventByFacilityPermissions)
    class EventByFacilityViewSet(viewsets.ModelViewSet):
    

    instead of:

    @permission_classes((EventByFacilityPermissions,))
    class EventByFacilityViewSet(viewsets.ModelViewSet):
    
    0 讨论(0)
  • 2020-12-30 23:18

    Just to let others know, I kept getting this same error and found that I forgot to include a comma in my REST_FRAMEWORK. I had this:

    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated'
    ),
    

    instead of this:

    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    

    The comma defines this as a one-element tuple

    0 讨论(0)
提交回复
热议问题