简体   繁体   English

在 Django-Rest 框架中使用序列化程序

[英]working with serializers in the Django-Rest Framework

I don't know is this a right platform to ask this question or not.我不知道这是问这个问题的合适平台。 I recently came across Django rest framework.我最近遇到了 Django rest 框架。 I am using django for one of my projects.I want to use DRF as an api client for my project.我在我的一个项目中使用 django。我想使用 DRF 作为我的项目的 api 客户端。 I came across this term serializing the model instances and serializers .我遇到了这个术语序列化模型实例序列化器 I went through DRF Tutorial , but I still have no idea of how it actually works.My questions are :我浏览了DRF 教程,但我仍然不知道它是如何工作的。我的问题是:

1.Why are serializers used? 1.为什么要使用序列化器? What purpose they serve?他们服务的目的是什么?

2.What more can be done using serializers? 2. 使用序列化器还能做什么?

3.How do serializers work? 3.序列化器是如何工作的?

There may be lots of references on this present in the internet but I want a simple and easy-to-understand answer to this.互联网上可能有很多关于此的参考资料,但我想要一个简单易懂的答案。 Thanks!!谢谢!!

1.Why are serializers used? 1.为什么要使用序列化器? What purpose they serve?他们服务的目的是什么?

In a very general way:以一种非常普遍的方式:

Imagine you want to invite your friend for a coffee.想象一下,您想邀请您的朋友喝杯咖啡。 To inform your friend, you text "what: coffee, time: 12h, where: The fancy coffee bar on the corner".要通知您的朋友,您可以发短信“什么:咖啡,时间:12 小时,地点:拐角处的高档咖啡吧”。 This text is your serialization of your idea.这篇文字是你对你的想法的连载。

The DRF does this for your Django models. DRF 为您的 Django 模型执行此操作。

Imagine you have a JS client that doesn't (want to) know about your DB or Django models.假设您有一个 JS 客户端不(想要)了解您的 DB 或 Django 模型。 You serialize your Django objects into JSON to have that data available in a JS compatible way:您将 Django 对象序列化为 JSON,以便以 JS 兼容的方式提供该数据:

data = {
    "address": {
        "street": "Fancy Street 1b"
    }
};

DRF offers an easy way to translate Django model instances into JSON (or other formats). DRF 提供了一种将 Django 模型实例转换为 JSON(或其他格式)的简单方法。 And that is what serializers do.这就是序列化程序所做的。 They also offer to deserialize what your JS frontend sends back to your server.他们还提供反序列化您的 JS 前端发送回您的服务器的内容。

2.What more can be done using serializers? 2. 使用序列化器还能做什么?

You can add more fields that are computed at serialization time.您可以添加更多在序列化时计算的字段。 For example, instead of adding simple IDs for related objects you can also serialize the related objects and add them nested into the JSON (or XML or whatever format).例如,除了为相关对象添加简单的 ID,您还可以序列化相关对象并将它们嵌套添加到 JSON(或 XML 或任何格式)中。

You can also add fields that you only need on the frontend side for some view, like things related to paging etc.您还可以添加仅在前端需要的某些视图的字段,例如与分页等相关的内容。

3.How do serializers work? 3.序列化器是如何工作的?

For python, the simplest serializer from python to JSON would be one that takes a python dictionary and simply creates string keys from the python keys, translates Django boolean into true and false strings, python None into null etc.对于 python,从 python 到 JSON 的最简单的序列化程序是使用 python 字典并简单地从 python 键创建字符串键,将 Django boolean 转换为truefalse字符串,将 python None转换为null等。

For more complex translations, you might want to add your own logic.对于更复杂的翻译,您可能需要添加自己的逻辑。 Eg like resolving related object references on Django models.例如,像解析 Django 模型上的相关对象引用。 Or formatting date objects into ISO standard strings (with timezone), localizing (decimal) numbers, localizing texts in general etc.或者将日期对象格式化为 ISO 标准字符串(带时区)、本地化(十进制)数字、本地化文本等。

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

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