如何在Django中编写用于注册的API

时间:2019-05-11 06:13:05

标签: django django-rest-framework django-views

我正在尝试编写用于注册的API。但是它向管理员添加数据,但是我想将数据存储到MySQL数据库。帮助我为此编写代码。

serializers.py

class SignupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Signup
        fields = ('username',
                  'phone_number',
                  'email',
                  'password',
                  'address'
                  )

views.py

class SignupAPIView(APIView):

    def get(self, request):
        signup = Signup.objects.all()
        print(signup)
        serializer = SignupSerializer(signup, many=True)
        print(serializer)
        return Response(serializer.data, status=200)

    def post(self, request):
        data = request.data
        print((data))
        serializer = SignupSerializer(data=data)
        print(serializer)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=201)
        return Response(serializer.error, status=400)

当我发布数据时,它将添加到Admin文件中而不是添加到MySQL数据库中。我想将此POST数据添加到MySQL数据库。

0 个答案:

没有答案