Django-rest-framework 02(序列化器原理理解)

Django-rest-framework 02

    • 1、创建一个新应用
    • 2、手动序列化器Serializer
      • 2.1、新建表Person
      • 2.2、视图
      • 2.3、路由
      • 2.4、新建serializers.py
      • 2.5、返回json类型数据
      • 2.6、测试
      • 2.7、多个数据序列化
    • 3、模型序列化器ModelSerializer
      • 3.1、新建表Student
      • 3.2、serializers.py
      • 3.3、测试

1、创建一个新应用

新创建一个应用

python manage.py startapp RESTs

注册
Django-rest-framework 02(序列化器原理理解)_第1张图片

2、手动序列化器Serializer

2.1、新建表Person

RESTs/models.py

from django.db import models


class Person(models.Model):
    p_name = models.CharField(max_length=32)
    p_age = models.IntegerField(default=1)
    p_sex = models.BooleanField(default=False)

迁移

python manage.py makemigrations
python manage.py migrate

2.2、视图

RESTs/views.py

from django.views import View


class PersonView(View):
    def get(self, request):
        pass

    def post(self, request):
        pass

2.3、路由

untitled/urls.py

Django-rest-framework 02(序列化器原理理解)_第2张图片
RESTs/urls.py
Django-rest-framework 02(序列化器原理理解)_第3张图片

2.4、新建serializers.py

from RESTs.models import Person
from rest_framework import serializers


# 手动序列化器
class PersonSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    p_name = serializers.CharField(max_length=10)
    p_sex = serializers.BooleanField(default=False)
    p_age = serializers.IntegerField(default=1)

    def create(self, validated_data):
        return Person.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.p_name = validated_data.get('p_name', instance.p_name)
        instance.p_age = validated_data.get('p_age', instance.p_age)
        instance.p_sex = validated_data.get('p_sex', instance.p_sex)
        instance.save()

        return instancez

手动测试序列化器

(REST) F:\python\yhpython\untitled>python manage.py shell
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from RESTs.models import Person
>>> person = Person()
>>> person.p_name='suck'
>>> person.p_age=6
>>> person.save()
>>> from RESTs.serializers import PersonSerializer
>>> person_serializer = PersonSerializer(person)
>>> person_serializer.data

{'id': 2, 'p_name': 'suck', 'p_sex': False, 'p_age': 6}

2.5、返回json类型数据

Django-rest-framework 02(序列化器原理理解)_第4张图片

2.6、测试

Django-rest-framework 02(序列化器原理理解)_第5张图片
Django-rest-framework 02(序列化器原理理解)_第6张图片

2.7、多个数据序列化

Django-rest-framework 02(序列化器原理理解)_第7张图片
Django-rest-framework 02(序列化器原理理解)_第8张图片

3、模型序列化器ModelSerializer

3.1、新建表Student

Django-rest-framework 02(序列化器原理理解)_第9张图片
迁移

python manage.py makemigrations
python manage.py migrate

3.2、serializers.py

Django-rest-framework 02(序列化器原理理解)_第10张图片
视图
Django-rest-framework 02(序列化器原理理解)_第11张图片
路由
Django-rest-framework 02(序列化器原理理解)_第12张图片

3.3、测试

运行
Django-rest-framework 02(序列化器原理理解)_第13张图片
发起post
Django-rest-framework 02(序列化器原理理解)_第14张图片

你可能感兴趣的:(#,Django)