这里使用 Django Shell 来测试
python manage.py shell
单条记录序列化
from categories.models import Category
from categories.serializers import CategorySerializer
category = Category.objects.get(id=1)
serializer = CategorySerializer(category)
serializer.data
多条记录序列化
categories = Category.objects.all()
serializer = CategorySerializer(categories, many=True)
serializer.data
关联表的序列化
courses 中,定义 models.py
from django.db import models
from ca...