[python] web application development DRF framework

DRF

[python] web application development DRF framework

Django rest_framework, referred to as drf, can more conveniently use django to write interfaces that conform to the RESTful specification, (reducing the code for writing api interfaces)

Django REST framework is a web application development framework (an app of Django) based on Django. It can quickly develop REST API interface and apply it
in REST framework. It provides the definition of serializer Serialzier, which can help us simplify the sequence The process of serialization and deserialization
is not only that, but also provides rich class views, extended classes, and view sets to simplify the writing of views. REST
framework also provides functions such as authentication, permissions, current limiting, filtering, paging, and interface documents to support
REST The framework provides an API Web visualization interface to facilitate viewing of the test interface

2. Installation and quick use (csrf disabled state)

  • install drf
pip3 install djangorestframework
  • The models.py file writes model classes (requiring persistence)
from django.db import models


class Book(models.Model):
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=8,decimal_places=2)
    author = models.CharField(max_length=16)
    publish = models.CharField(max_length=16)
# 创建好模型类执行数据库迁移命令
  • serializers.py creates a new serialization class (the file is created by itself), if it inherits ModelSerializer (serialization does not mean that it must be stored in the database)
from rest_framework.serializers import ModelSerializer
from drf_test import models


class BookSerializers(ModelSerializer):
    class Meta:
        model = models.Book  #  指明该序列化器处理的数据字段从模型类Book参考生成
        fields = "__all__"   # 指明该序列化器包含模型类中的哪些字段,’all‘指明包含所有字段

If the class inherits Serializer, that is, inherits the base class, then the writing method of the class we want to write is similar to that in the model, and the format is as follows:

Use of serialization

When writing logic in the view, for example, if we want to return the queried data to the previous section, then we need to serialize the queried data, then we need to use a serializer, and the usage method is as follows:

If the data is passed from the front end, what should I do if I want to deserialize it, just change the attribute of instace to the attribute of data, but the attributes are different, and the others are the same.

The specific source code is:

Return the queried data to the previous section, it is possible to query multiple pieces of data, why did it return:

 book_data = serializers.BookModelSerializers(book_obj).data  #单条数据序列化 

book_data = serializers.BookModelSerializers(book_query,many=True).data  
#不管是一条还是多条,只要数据是被[]嵌套,都要写many=True

  • views.py write view class
 
 

Reference: DRF Framework (4) - Serializer and Deserializer - Tencent Cloud Developer Community - Tencent Cloud

2. Introduction to DRF framework

Guess you like

Origin blog.csdn.net/qq_35789269/article/details/131407133
Recommended