1、下载安装第三方包:
pip install djangorestframework
2、注册:
3、序列化
创建文件 serializers.py
from rest_framework import serializers
from user.models import UserInfo
class UserSeralizer1(serializers.Serializer):
name = serializers.CharField(max_length=20)
age = serializers.IntegerField()
phone = serializers.CharField(max_length=11)
def create(self, validated_data):
user_obj = UserInfo.objects.create(**validated_data)
return user_obj
def update(self, instance, validated_data):
print('进入更新操作')
instance.name=validated_data.get('name')
instance.age=validated_data.get('age')
instance.phone=validated_data.get('phone')
instance.save()
return instance