0Pricing
Django Academy · 课时

序列化器:从模型到 JSON

将对象转换为 JSON,或从 JSON 转换回来

序列化器:从模型到 JSON 是 CoddyKit 上的免费 Django Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Translator Layer

A serializer is DRF's translator: it converts your model instances into JSON to send out, and parses incoming JSON back into Python.

Why Not Just json.dumps?

Raw json.dumps cannot handle model objects or validation. A serializer understands fields, types, and rules, so it does the heavy lifting for you.

Meet ModelSerializer

The fastest start is ModelSerializer: point it at a model and it generates matching fields automatically, just like a ModelForm does.

from rest_framework import serializers

Declare One

You subclass ModelSerializer and use an inner Meta class to name the model and the fields you want exposed in the API.

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = ['id', 'title']

Choosing Fields

List fields explicitly or use fields = '__all__' to include them all. Being explicit is safer because it never leaks new columns by accident.

Serialize to JSON

Pass an instance to the serializer and read its .data attribute to get a ready-to-render Python dict of plain JSON-friendly values.

ser = BookSerializer(book)
ser.data  # {'id': 1, 'title': 'Dune'}

Many Objects at Once

To serialize a whole queryset, add many=True and DRF returns a list of dicts, one per object, in a single step.

BookSerializer(books, many=True).data

Deserialize Input

Going the other way, you pass data= with incoming JSON so the serializer can parse and later validate what a client sent.

ser = BookSerializer(data=request.data)

Validate It

Call is_valid() before trusting input. It runs every field's checks and fills validated_data, or populates errors you can return.

ser.is_valid()
ser.errors

Save Cleanly

Once valid, save() creates or updates the model instance, so a serializer handles both translation and persistence in one object.

ser.save()

Forms, but for APIs

Think of a serializer as a form for APIs: it renders data out, validates data in, and saves it, all without any HTML at all.

Quick Check

What does .data give you on a model serializer?

Recap: Serializers

You learned that a ModelSerializer turns models into JSON, validates incoming data, and saves it, acting as the bridge of your API. 🔁

常见问题解答

「序列化器:从模型到 JSON」课时是免费的吗?

是的 — 「序列化器:从模型到 JSON」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。

「序列化器:从模型到 JSON」这节课中我会学到什么?

将对象转换为 JSON,或从 JSON 转换回来 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Django Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「序列化器:从模型到 JSON」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Django Academy 课中编写并运行代码吗?

能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 安装和配置 DRF
  2. 序列化器:从模型到 JSON
  3. APIView 与函数式 @api_view
  4. 可浏览 API 与状态码
← 返回 Django Academy