前提:
使用的前提条件为必须有一个model模型类.
恰好想要使用的序列化器对应的是Django的模型类,那么DRF为我们提供了ModelSerializer模型类序列化器来帮助我们快速创建一个Serializer类.
ModelSerializer与常规的Serializer相同,但提供了:
1.基于模型类自动生成一系列字段
2.包含默认的create()和update()的的实现
当我们的序列化器是继承自serializers.Serializer 时:
是没有create() update()的需要自己去序列化器中去定义.
这时使用ModelSerializer类型即:继承自serializers.ModelSerializer
使用Ctrl+b进入到ModelSerializer中去查看封装的函数.
class ModelSerializer(Serializer):
A `ModelSerializer` is just a regular `Serializer`, except that:
A set of default fields are automatically populated.
A set of default validators are automatically populated.
Default `.create()` and `.update()` implementations are provided.
The process of automatically determining a set of serializer fields
based on the model fields is reasonably complex, but you
almost certainly don't need to dig into the implementation.
If the &#