python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Django drf框架序列化 视图

基于Django drf框架序列化视图

作者:子午

这篇文章主要为大家介绍了基于Django drf框架序列化视图,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

介绍

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. 效果显示

序列化关联返回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']

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']

3. 嵌套序列化器的使用

class BookSerializer(serializers.ModelSerializer):
    # Heroserializer应先于本序列化器前定义
    book_hero = HeroSerializer(many=True)
    class Meta:
        model = BookModel
        fields = ['name', 'book_info', 'book_hero', 'create_time']

4. read_only和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/

以上就是基于Django drf框架序列化视图的详细内容,更多关于Django drf框架序列化 视图的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文