如何从序列化类RestApi中检索数据

时间:2019-11-02 07:44:32

标签: python django django-rest-framework

在登录功能中,正在获取令牌,我需要获取客户端的所有详细信息。

我不知道如何从ClientSerializer类获取数据。

如果我尝试引发错误“ SerializerMetaclass类型的对象不可JSON序列化”

View.py

 class Client_view(generics.ListCreateAPIView, generics.RetrieveUpdateDestroyAPIView):
        authentication_classes = [SessionAuthentication, BasicAuthentication, TokenAuthentication]
        permission_classes = [IsAuthenticated]
        queryset = Client.objects.all()
        serializer_class = ClientSerializer


class LoginView(APIView):

    def post(self, request):

        serializer = LoginSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data["user"]
        profile=ClientSerializer
        django_login(request, user)
        token, created = Token.objects.get_or_create(user=user)
        return Response( {"token":Token.key},{"Here i Need to Print All profile Details"},{"username":user.username}, status=200)

Serializer.py

class ClientSerializer(serializers.ModelSerializer):
    class Meta:
        fields = ['id', 'Name', 'UserName', 'Email', 'Mobile', 'Address']

        model = models.Client


class LoginSerializer(serializers.Serializer):
    username = serializers.CharField()
    password = serializers.CharField()

    def validate(self, data):
        username = data.get("username", "")
        password = data.get("password", "")

        if username and password:
            user = authenticate(username=username, password=password)
            if user:
                if user.is_active:
                    data["user"] = user
                else:
                    msg = "User is deactivated."
                    raise exceptions.ValidationError(msg)
            else:
                msg = "Unable to login with given credentials."
                raise exceptions.ValidationError(msg)
        else:
            msg = "Must provide username and password both."
            raise exceptions.ValidationError(msg)
        return data

0 个答案:

没有答案