sri ganesh
sri ganesh

Reputation: 71

'type' object is not iterable with Django Rest Framework and django oauth2-toolkit

Well I am trying to create new access token for the login user on creation with custom authentication class in views.

Serializer.py

class UserCreateSerializer(ModelSerializer):            

    def create(self, validated_data):
        user = User.objects.create_user(validated_data['username'], 
            validated_data['email'],
            validated_data['password'])
        return user

    class Meta:
        model = User
        fields = ('username', 'email' ,'password')

views.py

class User_Create_view(CreateAPIView):
    serializer_class = UserCreateSerializer
    queryset = User.objects.all()
    permission_classes = [AllowAny]
    authentication_classes = Has_Access_Token

    def create(self, request):
        serializers =self.serializer_class(data=request.data)
        if serializers.is_valid():
        # pdb.set_trace()
            serializers.save()   
            # Has_Access_Token.access_token(Has_Access_Token())         
            return Response(serializers.data)
        return Response(status=status.HTTP_202_ACCEPTED))

permission.py

class Has_Access_Token(BaseAuthentication):
    def access_token(self):
        app = Application.objects.get(name="testing")
        tok = generate_token()
        pdb.set_trace()
        acce_token=AccessToken.objects.get_or_create(
        user=User.objects.all().last(),
        application=app,
        expires=datetime.datetime.now() + datetime.timedelta(days=365),
        token=tok)
        return acce_token

    @method_decorator(access_token)
    def authenticate(self):
        return request

If I use the Decorator

File "/usr/lib/python2.7/functools.py", line 33, in update_wrapper setattr(wrapper, attr, getattr(wrapped, attr)) AttributeError: 'tuple' object has no attribute 'module'

If I am Not using the Decorator File "/home/allwin/Desktop/response/env/local/lib/python2.7/site-packages/rest_framework/views.py", line 262, in get_authenticators return [auth() for auth in self.authentication_classes] TypeError: 'type' object is not iterable

The Problem I am facing is that when i use the Has_Access_Token fuction implicitly after serializer.save() Access token is generated in admin with respect to user but that's not effective method, so I need to override the custom authentication_class in views.

Could somebody please suggest some ways to tackle this issue or perhaps let me know the decorator correction with the above code.

Thanks in advance.

Upvotes: 2

Views: 1352

Answers (1)

Kaviarasan K K
Kaviarasan K K

Reputation: 69

While setting REST_FRAMEWORK.DEFAULT_AUTHENTICATION_CLASSES in settings.py file, the customAuthencationClass must like below:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [ # need to be list not tuple
        'CustomAuthentication',
    ],
}

Upvotes: 3

Related Questions