AttributeError: 'ReturnDict' object has no attribute 'pk'
<p>I am trying to design an api for user login and logout. But I am facing an error of AttributeError: 'ReturnDict' object has no attribute 'pk'. What might be creating this error?</p>

<p><a href="https://i.stack.imgur.com/O8nMi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/O8nMi.png" alt="enter image description here"></a></p>

<p><strong>traceback in detail</strong></p>

<pre><code>File "/home/tushant/Projects/nepMart/account/api/views.py", line 61, in post
    login(request, serializer.data)
  File "/home/tushant/.virtualenvs/NepMart/lib/python3.5/site-packages/django/contrib/auth/__init__.py", line 102, in login
    if _get_user_session_key(request) != user.pk or (
AttributeError: 'ReturnDict' object has no attribute 'pk'
</code></pre>

<p><strong>Serializers.py</strong></p>

<pre><code>class UserLoginSerializer(ModelSerializer):
    token = CharField(allow_blank=True, read_only=True)
    username = CharField(required=False, allow_blank=True)
    # email = EmailField(label='Email Address',required=False, allow_blank=True)
    remember = BooleanField(default=True, help_text = ("If checked you will stay logged in for 3 weeks"))
    class Meta:
        model = User
        fields = [
            'username',
            # 'email',
            'password',
            'remember',
            'token',

        ]
        extra_kwargs = {"password":
                            {"write_only": True}
                            }
</code></pre>

<p><strong>views.py</strong></p>

<pre><code>class UserLoginAPIView(APIView):
permission_classes = [AllowAny]
serializer_class = UserLoginSerializer

def post(self, request, *args, **kwargs):
    data = request.data
    serializer = UserLoginSerializer(data=data)
    if serializer.is_valid(raise_exception=True):
        # new_data = serializer.data
        print('request.data', request.data)
        print('serializer.data',serializer.data)
        login(request, serializer.data)
        if data.get('remember'):
            request.session.set_expiry(60 * 60 * 24 * 7 * 3)
        else:
            request.session.set_expiry(0)
        return Response({
            'detail': ('Logged in successfully'),
            # TODO: maybe more user info in the request would have sense
            'username': serializer.data.username
        })
    return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
</code></pre>

<p><strong>ifincase usercreation is needed</strong></p>

<pre><code>class UserCreateSerializer(ModelSerializer):
    email = EmailField(label='Email Address')
    email2 = EmailField(label='Confirm Email Address')
    class Meta:
        model = User
        fields = [
            'username',
            'id',
            'password',
            'email',
            'email2',
        ]
        extra_kwargs = {"password":
                            {"write_only": True}
                        }

    def create(self, validated_data):
        print('validated_data',validated_data)
        username = validated_data['username']
        email = validated_data['email']
        email2 = validated_data['email2']
        password = validated_data['password']
        user_obj = User(
                username=username,
                email=email
            )
        user_obj.set_password(password)
        user_obj.save()
        return validated_data
</code></pre>