DAY 143 DRF-Django rest framework

DRF-Django rest framework

1. 修改删除接口

views.py

DAY 143 DRF-Django rest framework

serializer.py

DAY 143 DRF-Django rest framework

2. 高级用法之source

DAY 143 DRF-Django rest framework

3. 模型类序列化器

DAY 143 DRF-Django rest framework

4. 高级用法之SerializerMethodField

# 方式一 BookSerializer + SerializerMethodField
class BookSerializer(serializers.Serializer):
  id = serializers.IntegerField(required=False)
  name = serializers.CharField(max_length=32,min_length=2,source=‘title‘)
  price = serializers.DecimalField(max_digits=5, decimal_places=2)
  publish = serializers.SerializerMethodField()
  def get_publish(self,obj):
    dic={‘name‘:obj.publish.name,‘addr‘:obj.publish.addr}
    return dic

# 方式二 BookModelSerializer + SerializerMethodField
class BookModelSerializer(serializers.ModelSerializer):
  class Meta:
    model = models.Book
    fields = ‘__all__‘
  publish = serializers.SerializerMethodField()
  def get_publish(self,obj):
    dic={‘name‘:obj.publish.name,‘addr‘:obj.publish.addr}
    return dic

# 方式三 使用序列化类的嵌套
class PublishSerializer(serializers.ModelSerializer):
  class Meta:
    model = models.Publish
    # fields = ‘__all__‘
    fields = [‘name‘,‘addr‘]

class BookModelSerializer(serializers.ModelSerializer):
  publish = PublishSerializer()
  class Meta:
    model = models.Book
    fields = ‘__all__‘

5. drf的请求与相应

# Request
-data :前端以post请求提交的数据都在它中
-FILES :前端提交的文件
-query_params:就是原来的request.GET
-重写了 __getattr__
-使用新的request.method其实取得就是原生request.method(通过反射实现)

# Response
-from rest_framework.response import Response
-data:响应的字典
-status:http响应的状态码
-drf提供给你了所有的状态码,以及它的意思
from rest_framework.status import HTTP_201_CREATED
-template_name:模板名字(一般不动),了解
-headers:响应头,字典
-content_type:响应的编码方式,了解

# 自己封装一个Response对象
class CommonResponse:
  def __init__(self):
    self.code=100
    self.msg=‘‘
  @property
  def get_dic(self):
    return self.__dict__

# 通过配置,选择默认模板的显示形式(浏览器方式,json方式)
-配置文件方式(全局)
-如果没有配置,默认有浏览器和json
-drf有默认配置文件
from rest_framework.settings import DEFAULTS
REST_FRAMEWORK = {
‘DEFAULT_RENDERER_CLASSES‘: ( # 默认响应渲染类
‘rest_framework.renderers.JSONRenderer‘, # json渲染器
‘rest_framework.renderers.BrowsableAPIRenderer‘, # 浏览API渲染器
)}
-在视图类中配置(局部)
-粒度更小
-class BookDetail(APIView):
renderer_classes=[JSONRenderer,]

6. many=True

DAY 143 DRF-Django rest framework

7. 局部全局钩子源码解析

# 局部全局钩子源码解析
入口是is_valid()
=> BaseSerializer => is_valid
=> self._validated_data
=> self.run_validation(self.initial_data)
=> Serializer这个类的:self.run_validation

# 重写父类Field的run_validation方法
class Serializer(BaseSerializer, metaclass=SerializerMetaclass):
  ......

  def run_validation(self, data=empty):
  """
  We override the default `run_validation`, because the validation
  performed by validators and the `.validate()` method should
  be coerced into an error dictionary with a ‘non_fields_error‘ key.
  """
  # 验证空值
  (is_empty_value, data) = self.validate_empty_values(data)
  # 是空值返回data
  if is_empty_value:
    return data
  # 局部字段的校验和局部钩子校验 返回OrderedDict()实例化对象
  value = self.to_internal_value(data)
  try:
    # 运行验证器
    self.run_validators(value)
    # 全局钩子的校验
    value = self.validate(value)
    """
    这里的全局钩子如果我们需要使用的时候,需要重写,重写的时候一定要返回被拿出来验证的属性
    def validate(self, attrs):
      return attrs
    """
  except (ValidationError, DjangoValidationError) as exc:
    raise ValidationError(detail=as_serializer_error(exc))
  return value

 

DAY 143 DRF-Django rest framework

上一篇:STL常用容器用法


下一篇:android引入百度地图之最简单的例子-HelloBaiDuMap