当前位置:网站首页>Unit 12 associated serialization
Unit 12 associated serialization
2022-08-02 14:19:00 【czy1206527605】
One. Default foreign key serialization
1. Now suppose we have two tables
class Teacher(models.Model):tea_name = models.CharField(max_length=20,verbose_name='teacher name')class Meta:verbose_name = 'Teacher table'verbose_name_plural = verbose_namedb_table = 'teacher'def __str__(self):return self.tea_nameclass Student(models.Model):stu_name = models.CharField(max_length=20,verbose_name='student name')score = models.IntegerField(verbose_name='score')sex = models.CharField(max_length=5,verbose_name='sex')teacher = models.ForeignKey(to=Teacher, on_delete=models.CASCADE, verbose_name='Teacher')# teacher's foreign key from table defaults to tea_id, not tea_nameclass Meta:verbose_name = 'student table'verbose_name_plural = verbose_namedb_table = 'student'def __str__(self):return self.stu_nameOur foreign key in the slave table student will generally be displayed as the id of the main table
How to change the foreign key display from id to the content of the binding field of the main table?
2. Module preparation
from rest_framework import serializersfrom student.models import Teacher,Student1.StringRelated foreign key serialization
Return the __str__ method of the associated table as the result, setting read_only means that the field will not be deserialized and verified
Use the str method of the foreign key table to serialize
class StudentSerializer2(serializers.ModelSerializer):teacher = serializers.StringRelatedField()class Meta:model = Studentfields = '__all__'2.SlugRelated foreign key serialization
Specified field serialization Changes the content of a field to the content of the specified display field
class StudentSerializer2(serializers.ModelSerializer):teacher = serializers.SlugRelatedField(read_only=True,slug_field='tea_name')class Meta:model = Studentfields = '__all__'3.PrimaryKeyRelated foreign key serialization
PrimaryKeyRelatedField foreign key serialization, using the primary key of the foreign key table for serialization.Same as the original model serializer
class StudentSerializer2(serializers.ModelSerializer):teacher = serializers.PrimaryKeyRelatedField(read_only=True)class Meta:model = Studentfields = '__all__'4. Foreign key custom serialization method
Use custom method for serialization, field customization, must be a field in the database (name it yourself)
class StudentSerializer2(serializers.ModelSerializer):tea_name = serializers.SerializerMethodField(read_only=True)# obj is the object of the model classdef get_tea_name(self,obj):return obj.teacher.tea_nameclass Meta:model = Studentfields = '__all__'2. Deserialization of the associated serializer
The default serializer does not need to write any fields for deserialization. The default association field will accept an id data as a verification basis and create it
To put it bluntly, it is a normal model serializer
class StudentSerializer(serializers.ModelSerializer):class Meta:model = Studentfields = '__all__'边栏推荐
- Flask请求应用上下文源码分析
- RowBounds[通俗易懂]
- About the development forecast of the market outlook?2021-05-23
- Flask框架深入二
- yolov5,yolov4,yolov3 mess
- The most complete ever!A collection of 47 common terms of "digital transformation", read it in seconds~
- WeChat Mini Program-Recent Dynamic Scrolling Implementation
- mysql的case when如何用
- [ROS](06)ROS通信 —— 话题(Topic)通信
- 第四单元 路由层
猜你喜欢

Interview | with questions to learn, Apache DolphinScheduler Wang Fuzheng

专访|带着问题去学习,Apache DolphinScheduler 王福政

Unit 5 Hold Status

政策利空对行情没有长期影响,牛市仍将继续 2021-05-19

Unit 15 Paging, Filtering

【Tensorflow】AttributeError: ‘_TfDeviceCaptureOp‘ object has no attribute ‘_set_device_from_string‘

shell脚本“画画”

Sentinel源码(三)slot解析

8581 线性链表逆置

云GPU(恒源云)训练的具体操作流程
随机推荐
vim复制粘贴_vim如何复制粘贴
关于密码加密的一点思路
8581 Linear linked list inversion
深度学习框架pytorch快速开发与实战chapter4
Mysql's case the when you how to use
政策利空对行情没有长期影响,牛市仍将继续 2021-05-19
【ROS】编译软件包packages遇到进度缓慢或卡死,使用swap
Flask-SQLAlchemy
AWVS工具介绍[通俗易懂]
Flask项目的完整创建 七牛云与容联云
8576 Basic operations of sequential linear tables
[ROS] (04) Detailed explanation of package.xml
MobileNet ShuffleNet & yolov5替换backbone
The 2nd China Rust Developers Conference (RustChinaConf 2021~2022) Online Conference Officially Opens Registration
目标检测场景SSD-Mobilenetv1-FPN
监管再次重拳出击,后市如何?2021-05-22
Sentinel源码(一)SentinelResourceAspect
瑞吉外卖笔记——第05讲Redis入门
第十一单元 序列化器
STM32(F407)—— 堆栈