DRF

27 views
Skip to first unread message

Soumen Khatua

unread,
Sep 12, 2019, 6:55:55 AM9/12/19
to django...@googlegroups.com
Hi Folks,
I didn't understand this code, Cab you guys please explain me about this code:

views.py:
-------------
class UserLoginAPIView(GenericAPIView):
authentication_classes = ()
permission_classes = ()
serializer_class = UserLoginSerializer ()
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
user = serializer.user 
token, _ = Token.objects.get_or_create(user=user)
return Response(data=TokenSerializer(token).data, status=status.HTTP_200_OK)
else:
return Response(data=serializer.errors,status=status.HTTP_400_BAD_REQUEST,)


seralizers.py
-----------------
class UserLoginSerializer(serializers.Serializer):
    username = serializers.CharField(required=True)
    password = serializers.CharField(required=True)

    default_error_messages = {
        'inactive_account': _('User account is disabled.'),
        'invalid_credentials': _('Unable to login with provided credentials.')
    }

    def __init__(self, *args, **kwargs):
        super(UserLoginSerializer, self).__init__(*args, **kwargs)
        self.user = None

    def validate(self, attrs):
        self.user = authenticate(username=attrs.get("username"), password=attrs.get('password'))
        if self.user:
            if not self.user.is_active:
                raise serializers.ValidationError(self.error_messages['inactive_account'])
            return attrs
        else:
            raise serializers.ValidationError(self.error_messages['invalid_credentials']
 
Here , 
1)  super(UserLoginSerializer, self).__init__(*args, **kwargs)
 what  value  parent will get by using line super(UserLoginSerializer, self).__init__(*args, **kwargs) 
2) Validate is one method which take attrs as a argument but If I didn;t pass value of attrs then how it will be work?

Guys please help me to understand this code.

Thank You

Regards,
Soumen

Andréas Kühne

unread,
Sep 17, 2019, 3:20:56 AM9/17/19
to django...@googlegroups.com
Hi Soumen,

So let's look at your questions:
1. super(UserLoginSerializer, self).__init__(*args, **kwargs) - this means that it gets the parent of the UserLoginSerializer - which is serializers.Serializer. You can force python to choose another parent if you want to do something different, by changing the first parameter to super(). However, running on python 3, you don't need to add any parameters to the super call at all (unless you want to change which parent should be used). 
2. validate is not called by you directly. You are calling the "is_valid()" method. This method in it's turn calls the validate method with the correct parameters - so you don't need to worry about it. However the parameter name is a bit strange - see here for examples and a description:

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WZ3BGiHoZXmQRYiU%3DxCYdbPg%2BonCmwGSX0B%2B8VBb76aZw%40mail.gmail.com.

Soumen Khatua

unread,
Sep 17, 2019, 3:41:36 AM9/17/19
to django...@googlegroups.com
Thank you, It really helps me a lot.

Regards, 
Soumen

Reply all
Reply to author
Forward
0 new messages