且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

django.core序列化程序和Django Rest Framework序列化程序之间的区别

更新时间:2022-06-25 09:29:39

django.core 序列化程序旨在将整个模型实例序列化为XML,JSON或YAML,反之亦然。除了序列化之外,它们什么也没做。

django.core serializers are meant for the purpose of serializing entire model instances into XML, JSON, or YAML, and vice versa. They don't do anything besides just serializing.

DRF的序列化器专门用于在处理来自HTML表单或API请求的数据时将模型实例转换为JSON对象 / em>。因此,序列化并不总是一个平稳或直接的过程,因为您可能会传递非法或不完整的数据,或者表格的字段可能不以明显的方式与相应模型的字段相对应。因此,DRF允许您创建 serializers.Serializer 的自定义子类,以便清除和验证传递给服务器的数据。这还允许您自定义数据在模型实例中的存储方式。 请参阅此处的文档

DRF's serializers are specifically for converting model instances into JSON objects when dealing with data from HTML forms or API requests. Thus, serialization is not always a smooth or straightforward process, as you may be passed illegitimate or incomplete data, or fields of the form may not correspond in an obvious way to the fields of the corresponding model(s). For this reason, DRF allows you to create custom subclasses of serializers.Serializer in order to clean and validate data that is passed to the server. This also allows you to customize the manner in which the data is stored in the model instance. See the documentation here.