Use Django REST framework in brief

 

                                                                                                       

 

The official document: https: //www.django-rest-framework.org/

GitHub Source: https: //github.com/encode/django-rest-framework/tree/master

 

1. Install DRF

pip install djangorestframework

  

2. Add rest_framework application

We use demo projects created in the framework of learning Django, add 'rest_framework' in INSTALLED_APPS settings.py's.

INSTALLED_APPS = [
    ...
    'rest_framework',
]

  

Then you can use the DRF were developed.

 

Defined Serializer

Method 1. Definitions

Django REST framework in Serializer uses classes to define, must be inherited from rest_framework.serializers.Serializer.

For example, we already have a database model class BookInfo

class BookInfo(models.Model):
    btitle = models.CharField(max_length=20, verbose_name='名称')
    bpub_date = models.DateField(verbose_name='发布日期', null=True)
    bread = models.IntegerField(default=0, verbose_name='阅读量')
    bcomment = models.IntegerField(default=0, verbose_name='评论量')
    image = models.ImageField(upload_to='booktest', verbose_name='图片', null=True)

  

We want to provide a serializer for this model class can be defined as follows:

BookInfoSerializer class (serializers.Serializer): 
    "" "book data serializer" "" 
    ID = serializers.IntegerField (label = 'ID', READ_ONLY = True) 
    BTITLE = serializers.CharField (label = 'name', max_length = 20 ) 
    bpub_date = serializers.DateField (label = 'date', required = False) 
    Bread = serializers.IntegerField (label = 'amount of reading', required = False) 
    bcomment = serializers.IntegerField (label = 'comments amount', required = False) 
    Image = serializers.ImageField (label = 'pictures', required = False)

  

Note: serializer database model not only for the class definition, you can also define non-database data model class. serializer is independent of the presence database.

 

2. Fields and Options

Common field types:

Field Field construction way
BooleanField BooleanField()
NullBooleanField NullBooleanField()
CharField CharField(max_length=None, min_length=None, allow_blank=False, trim_whitespace=True)
EmailField EmailField(max_length=None, min_length=None, allow_blank=False)
RegexField RegexField(regex, max_length=None, min_length=None, allow_blank=False)
SlugField SlugField (max length = 50, min_length = None, allow_blank = False) 
regular field, to verify normal mode [A-zA-Z0-9
-] +
URLField URLField(max_length=200, min_length=None, allow_blank=False)
UUIDField UUIDField (= the format 'hex_verbose') 
the format: 
. 1)  'hex_verbose' As "5ce0e9a5-5ffa-654b-cee0-1238041fb31a" 
2)  'hex' As  "5ce0e9a55ffa654bcee01238041fb31a" 
3) 'int' - such as:  "123456789012312313134124512351145145114" 
4) 'urn' as: "urn:uuid:5ce0e9a5-5ffa-654b-cee0-1238041fb31a"
IPAddressField IPAddressField(protocol='both', unpack_ipv4=False, **options)
IntegerField IntegerField(max_value=None, min_value=None)
FloatField FloatField(max_value=None, min_value=None)
DecimalField DecimalField (max_digits, decimal_places, coerce_to_string = None, MAX_VALUE = None, MIN_VALUE = None)
max_digits: maximum number of digits
decimal_palces: decimal point position
DateTimeField DateTimeField(format=api_settings.DATETIME_FORMAT, input_formats=None)
DateField DateField(format=api_settings.DATE_FORMAT, input_formats=None)
TimeField TimeField(format=api_settings.TIME_FORMAT, input_formats=None)
DurationField DurationField()
ChoiceField ChoiceField (choices)
choices and usage of the same Django
MultipleChoiceField MultipleChoiceField(choices)
FileField FileField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL)
ImageField ImageField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL)
ListField ListField(child=<a_field_instance>, min_length=None, max_length=None)
DictField DictField(child=<a_field_instance>)

Option parameter:

参数名称 作用
max_length 最大长度
min_lenght 最小长度
allow_blank 是否允许为空
trim_whitespace 是否截断空白字符
max_value 最小值
min_value 最大值

 

Guess you like

Origin www.cnblogs.com/LiuXinyu12378/p/11247073.html