drf使用1(序列化,视图)

介绍

drf框架是基于Django框架,用于快速构建Web RESTful API的工具

官方文档:https://www.django-rest-framework.org/

安装:pip install djangorestframework

快速使用

1. 搭建模型

from django.db import models


class BookModel(models.Model):
    """
    书籍模型
    """
    name = models.CharField(max_length=200, verbose_name='书籍名称')
    book_info = models.TextField(max_length=1024, default=None, blank=True, verbose_name='书籍介绍')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'tb_book'
        verbose_name = '书籍信息'
        verbose_name_plural = verbose_name


class HeroModel(models.Model):
    """
    书籍人物模型
    """
    name = models.CharField(max_length=200, verbose_name='英雄姓名')
    hero_info = models.TextField(max_length=1024, default=None, blank=True, verbose_name='英雄介绍')
    join_time = models.DateTimeField(auto_now_add=True, verbose_name='加入时间')
    book_name = models.ForeignKey('BookModel', on_delete=models.CASCADE, related_name='book_hero', verbose_name='所属书籍')

    class Meta:
        db_table = 'tb_hero'
        verbose_name = '书籍人物信息'
        verbose_name_plural = verbose_name
  • 注意外键对应关系

2. setting配置

INSTALLED_APPS = [
    ......
    'rest_framework',
]

3. 序列化器serializer

# serializers.py

from rest_framework import serializers
from .models import BookModel, HeroModel


class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = BookModel
        fields = '__all__'

4. 视图views

# views.py

from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin, CreateModelMixin
from .serializers import BookSerializer
from .models import BookModel


class Books(GenericViewSet, ListModelMixin, CreateModelMixin):
    queryset = BookModel.objects.all()
    serializer_class = BookSerializer

5. 路由自动注册DefaultRouter

from rest_framework.routers import DefaultRouter
from . import views

app_name = 'book'
urlpatterns = [

]

router = DefaultRouter()
router.register('book', views.Books, basename='book')
urlpatterns += router.urls

6. 效果显示

drf使用1(序列化,视图)_第1张图片

drf使用1(序列化,视图)_第2张图片

序列化关联返回serializers

1. 返回关联外键的ID:PrimaryKeyRelatedField

class BookSerializer(serializers.ModelSerializer):
    book_hero = serializers.PrimaryKeyRelatedField(read_only=True, many=True)

    class Meta:
        model = BookModel
        fields = ['name', 'book_info', 'book_hero', 'create_time']

drf使用1(序列化,视图)_第3张图片

2. 返回关联字段模型的__str__方法的值:StringRelatedField

class BookSerializer(serializers.ModelSerializer):
    book_hero = serializers.StringRelatedField(read_only=True, many=True)

    class Meta:
        model = BookModel
        fields = ['name', 'book_info', 'book_hero', 'create_time']

drf使用1(序列化,视图)_第4张图片

3. 嵌套序列化器的使用

class BookSerializer(serializers.ModelSerializer):
    # Heroserializer应先于本序列化器前定义
    book_hero = HeroSerializer(many=True)

    class Meta:
        model = BookModel
        fields = ['name', 'book_info', 'book_hero', 'create_time']

drf使用1(序列化,视图)_第5张图片

4. read_only和write_only

  • read_only=True不参与反序列化验证过程,只参与序列化返回过程
  • write_only不参与序列化返回过程,只参与验证过程

5. 自定义验证方法(单/多字段验证)

  • 单一字段验证

    class BookSerializer(serializers.ModelSerializer):
        book_hero = serializers.StringRelatedField(many=True, read_only=True)
    
        class Meta:
            model = BookModel
            fields = ['name', 'book_info', 'book_hero', 'create_time']
    
        def validate_name(self, value):
            """
            验证书名不能是python
            """
            if value == 'python':
                raise serializers.ValidationError('书名不能是python')
            return value
  • 多字段验证

        def validate(self, attrs):
            if attrs['name'] == 'python' or attrs['book_info'] == 'python':
                raise serializers.ValidationError('书名或info不能是python')
            return attrs

6. self.action自定义装饰器使用

from rest_framework.decorators import action

class Books(GenericViewSet, ListModelMixin, CreateModelMixin):
    queryset = BookModel.objects.all()
    serializer_class = BookSerializer

    @action(methods=['get'], detail=True)
    def hello(self, request, pk):
        res = 'hello' + str(pk)
        return Response(res)

http://127.0.0.1:8000/book/1/hello/

drf使用1(序列化,视图)_第6张图片

你可能感兴趣的:(django)