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:
https://www.django-rest-framework.org/api-guide/serializers/#validation
https://www.django-rest-framework.org/api-guide/serializers/#object-level-validation

Med vänliga hälsningar,

Andréas


Den tors 12 sep. 2019 kl 12:55 skrev Soumen Khatua <
soumenkhatua...@gmail.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
>
> --
> 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+unsubscr...@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
> <https://groups.google.com/d/msgid/django-users/CAPUw6WZ3BGiHoZXmQRYiU%3DxCYdbPg%2BonCmwGSX0B%2B8VBb76aZw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK4qSCcLBzREDuTaz%2BGYX2yABadOnmAD6pw%3DVJcP6z3ULKk3WQ%40mail.gmail.com.

Reply via email to