当前位置:网站首页>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__'边栏推荐
- 音频处理:浮点型数据流转PCM文件
- 无序数组排序并得到最大间隔
- How does Apache, the world's largest open source foundation, work?
- 第五单元 保持状态
- Interview | with questions to learn, Apache DolphinScheduler Wang Fuzheng
- [ROS](03)CMakeLists.txt详解
- The most complete ever!A collection of 47 common terms of "digital transformation", read it in seconds~
- Sentinel源码(三)slot解析
- 一维卷积神经网络_卷积神经网络的基础知识「建议收藏」
- EasyExcel 的使用
猜你喜欢
随机推荐
yolov5,yolov4,yolov3 mess
Flask-RESTful请求响应与SQLAlchemy基础
The future of financial services will never stop, and the bull market will continue 2021-05-28
Some impressions of the 519 plummet 2021-05-21
8576 顺序线性表的基本操作
第五单元 保持状态
[ROS]ROS常用工具介绍(待续)
瑞吉外卖笔记——第05讲Redis入门
Interview | with questions to learn, Apache DolphinScheduler Wang Fuzheng
The 2nd China Rust Developers Conference (RustChinaConf 2021~2022) Online Conference Officially Opens Registration
动手学ocr(一)
vim复制粘贴_vim如何复制粘贴
8580 合并链表
你接受不了60%的暴跌,就没有资格获得6000%的涨幅 2021-05-27
replay视频播放器_怎么让手机音乐跟视频一起放
如何解决mysql服务无法启动1069
LayoutParams的详解
ZABBIX配置邮件报警和微信报警
php开源的客服系统_在线客服源码php
8581 线性链表逆置









