67 drf apiview response to the request parsing module rendering module sequencing module

1.Django Configuration Review

1. route distribution include

 

 

 2.app registration: when the app is not using the database-related operations, can not be registered; drf must be registered before use; in short registered like to add: Registration will load app package following __init__ file

 

 

 3. Database Configuration: configuration setting file

 

 3.orm database configuration

Common Fields

 

 Similar backstage expose the front desk and static resource files resource allocation static

 

 

 

Front-access

 

 

 2.APIView request lifecycle

 

3. The request analysis module

1. drf disabled csrf certification rules to dispatch process

 

2. Customize the data format configuration request drf

3. backward compatible

 

 

 

 

 

4. Response rendering module

Rendering data in two formats: data rendering and browser rendering (setting file to restframework View)

 

 

 

 Explanation: Comment out the browser and then choose the browser can only render the data the way, get json format data

The sequencing module

1. The introduction of the sequence module

 

 

2. Use

##### models.py
from django.db import models
from django.conf import settings
class User(models.Model):
    SEX_CHOICES = ((0, ''), (1, ''))
    name = models.CharField(max_length=64, verbose_name='姓名')
    password = models.CharField(max_length=64)
    age = models.IntegerField()
    height = models.DecimalField(max_digits=5, decimal_places=2, default=0)
    sex Models.IntegerField = (= SEX_CHOICES choices, default = 0)
     # Sex = models.CharField (choices = [( '0', 'M'), ( '1', 'F')]) 
    icon models.ImageField = ( = the upload_to ' icon ' , default = ' icon / Default.png ' ) 

    # field custom serialization to the front of the 
    # advantages: 1) the original database data may be formatted field 2) may be hidden outside the original field name database 3 ) can be directly connected to the operating table 
    @Property   # all fields are plugs 
    DEF Gender (Self):
         return self.get_sex_display () 

    @Property 
    DEF IMG (Self):
         return settings.BASE_URL settings.MEDIA_URL + + self.icon.name 

    DEF __str__ (Self):
         return self.name 
`` ` 

#### serializers.py sequence of core 
from rest_framework Import serializers
 from . Import Models
 class UserModelSerializer (serializers.ModelSerializer):
     class Meta:
         # The sequence is secondary to that class Model class 
        Model = models.User 
        # set of sequences which are involved with the deserialization field 
        # pluggable: field may be selectively returned to the front (plugs are manufactured in the Model class) 
        # fields = [ 'name', 'Age ',' height ',' Sex ',' icon] 
        Fields = [ ' name ' , ' Age ' ,'height', 'gender', 'img']
```

#### views.py
from rest_framework.views import APIView
from rest_framework.response import Response

from . import models, serializers

class UserAPIView(APIView):
    def get(self, request, *args, **kwargs):
        pk = kwargs.get('pk')
        if pk:  # 单查
            #1) interact with the database to get the resource or resource obj OBJS 
            # 2) into a sequence of data can be returned to the front json data 
            # 3) is returned to the reception data json 
            obj = models.User.objects.get (PK = PK) 
            Serializer = serializers .UserModelSerializer (obj, MANY = False)
             return the Response (serializer.data) 

        the else :   # cluster check 
            # 1) interact with the database to get the resource or resource obj OBJS 
            # 2) can be returned to the data sequence into data of the foreground json 
            # 3) json data is returned to the front 
            QuerySet = models.User.objects.all ()
             # whether the data is a plurality of operating many 
            Serializer = serializers.UserModelSerializer (QuerySet, many = True)
             Return the Response (serializer.data) 

    DEF POST (Self, request, args *, ** kwargs):
         # monocytogenes 
        # 1) to obtain data reception request from the request submitted by the 
        # 2) to convert the data into a Model object, and complete database storage operation 
        # 3) the successful storage of objects to be returned into the column json data reception (data not like request and response: Submit request requires a password, the password response must not show) 
        # 4) the data returned to the front json

 

Guess you like

Origin www.cnblogs.com/bigbox/p/12602602.html