简体   繁体   English

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

[英]Difference between django.core serializers and Django Rest Framework serializers

I am now learning Django and I just heard about Django Rest Framework (DRF). 我现在正在学习Django,我刚刚听说过Django Rest Framework(DRF)。 I was wondering what is the difference between the django.core serializers and the rest_framework serializers. 我想知道django.core序列化器和rest_framework序列化器之间有什么区别。 Yes, I know DRF is for APIs. 是的,我知道DRF适用于API。

django.core serializers are meant for the purpose of serializing entire model instances into XML, JSON, or YAML, and vice versa. django.core序列化程序用于将整个模型实例序列化为XML,JSON或YAML,反之亦然。 They don't do anything besides just serializing. 除了序列化之外,它们不做任何事情。

DRF's serializers are specifically for converting model instances into JSON objects when dealing with data from HTML forms or API requests . DRF的序列化程序专门用于在处理来自HTML表单或API请求的数据时将模型实例转换为JSON对象。 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. 因此,DRF允许您创建serializers.Serializer自定义子类,以便清理和验证传递给服务器的数据。 This also allows you to customize the manner in which the data is stored in the model instance. 这还允许您自定义数据在模型实例中的存储方式。 See the documentation here . 请参阅此处的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM