当前位置:网站首页>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__'
边栏推荐
猜你喜欢
paddleocr window10 first experience
史上最全!47个“数字化转型”常见术语合集,看完秒懂~
[ROS](02)创建&编译ROS软件包Package
Unit 5 Hold Status
Configure zabbix auto-discovery and auto-registration.
What are the file encryption software?Keep your files safe
数据机构---第六章图---图的遍历---选择题
政策利空对行情没有长期影响,牛市仍将继续 2021-05-19
How does Apache, the world's largest open source foundation, work?
(ROS) (03) CMakeLists. TXT, rounding
随机推荐
[ROS]ROS常用工具介绍(待续)
ZABBIX配置邮件报警和微信报警
Some impressions of the 519 plummet 2021-05-21
如何选择正规的期货交易平台开户?
[ROS]roscd和cd的区别
Data Organization---Chapter 6 Diagram---Graph Traversal---Multiple Choice Questions
Object detection scene SSD-Mobilenetv1-FPN
瑞吉外卖笔记——第10讲Swagger
Flask项目的完整创建 七牛云与容联云
Sentinel源码(三)slot解析
MySQL数据库语法格式
php开源的客服系统_在线客服源码php
网页设计(新手入门)[通俗易懂]
The bad policy has no long-term impact on the market, and the bull market will continue 2021-05-19
[ROS] The software package of the industrial computer does not compile
史上最全!47个“数字化转型”常见术语合集,看完秒懂~
第十五单元 分页、过滤
Flask框架
How does Apache, the world's largest open source foundation, work?
动手学ocr(一)