当前位置:网站首页>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_name
Our 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,Student
1.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__'
边栏推荐
- The world's largest Apache open source foundation is how it works?
- 线代:已知一个特征向量快速求另外两个与之正交的特征向量
- [ROS](04)package.xml详解
- Go语言初始
- 8576 顺序线性表的基本操作
- window10下半自动标注
- redis分布式锁和看门狗的实现
- [ROS] The software package of the industrial computer does not compile
- The 2nd China Rust Developers Conference (RustChinaConf 2021~2022) Online Conference Officially Opens Registration
- VMM是什么?_兮是什么意思
猜你喜欢
随机推荐
VMM是什么?_兮是什么意思
drf源码分析与全局捕获异常
Object detection scene SSD-Mobilenetv1-FPN
配置zabbix自动发现和自动注册。
8576 顺序线性表的基本操作
目标检测场景SSD-Mobilenetv1-FPN
第二届中国Rust开发者大会(RustChinaConf 2021~2022)线上大会正式开启报名
[ROS] (06) ROS Communication - Topic Communication
The most complete ever!A collection of 47 common terms of "digital transformation", read it in seconds~
第十五单元 分页、过滤
Flask框架
yolov5改进(一) 添加注意力集中机制
文件加密软件有哪些?保障你的文件安全
hsql是什么_MQL语言
网络剪枝(1)
理解TCP长连接(Keepalive)
[ROS] The difference between roscd and cd
Basic operations of 8583 sequential stack
MarkDown语法汇总
动手学ocr(一)