当前位置:网站首页>restframework读取和非读取序列处理
restframework读取和非读取序列处理
2022-06-22 04:58:00 【laoli815】
使用restframework的时候,网页前端如果只是单纯get请求的话,会涉及到一些我们自定义方法的字段,如果在这个页面在提交就会报错,所以序列的时候可以区分开这些需求,设计两个序列化,get请求序列化和put,patch,delete,post序列化
serializers.py
class BookReadSerializer(serializers.ModelSerializer):
category = CategorySerializer()
author = AuthorSerializer()
class Meta:
model = Book
fields = '__all__'
class BookWriteSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = "__all__"
views.py
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
def get_serializer_class(self):
if self.request.method in ['GET', 'HEAD']:
return BookReadSerializer
else:
return BookWriteSerializer
直接区分开了,不同的请求方法调用不同序列
边栏推荐
- Ora - 15063: ASM discovered an insufficient number of Disks for diskgroup Recovery - - -
- Recruiters - issue 23
- 103. simple chat room 6: using socket communication
- 这是一个图片
- [user guide] use of Tsinghua source
- numpy库常用知识整理
- 数据库---基础知识
- How much does it cost to buy a fixed-term life insurance with an insured amount of 500000 at the age of 42? Is there any product recommendation
- LeetCode——二叉搜索树的第k大节点(借助中序遍历)
- Qt保存QTextEdit内存至.txt文件中
猜你喜欢
![[scientific research notes] focal loss](/img/ca/4a30fd925b87ed2baa2523d8dbf59d.png)
[scientific research notes] focal loss

Ora - 15063: ASM discovered an insufficient number of Disks for diskgroup Recovery - - -

Target detection algorithm based on deep learning interview essential (rcnn~yolov5)

加快推进工业互联网,图扑“智”绘发展新蓝图

exness:欧洲央行行长拉加德重申计划在7月会议上加息

Systematic arrangement | how many children's shoes have forgotten to be done carefully before the model development (practical operation)

【故障诊断】stitch.py脚本失效

爬梯子&&卖卖股份的最佳时期(跑路人笔记)

Progress information collection for progress control of Zhiyuan project management SPM system

LeetCode——二叉搜索树的第k大节点(借助中序遍历)
随机推荐
The best time to climb a ladder & sell shares (notes of the runner)
【故障诊断】cv2.imwrite无法写入图片,但程序就是不报错
下拉刷新,上推加载(简单好用,终于会了)
Software architecture and pattern: structure, component and relationship
JUC - thread interrupt and thread waiting and wakeup (locksupport)
【科研笔记】关于使用openslide切图的下采样倍数
103.简易聊天室6:使用 Socket 通信
MySQL common SQL
uwsgi-invalid-request-block-size invalid request block size: 21327 (max 4096)...skip 的解决办法
mysql笔记
Redis 主从复制
The Impossible Triangle of NLP?
In depth understanding of JS delete
LeetCode 437. Path sum III - binary tree series question 13
mysql day03课堂笔记
Overrides vs overloads of methods
It is easy to analyze and improve R & D efficiency by understanding these five figures
103. simple chat room 6: using socket communication
浏览器--常用的搜索操作符大全--使用/实例
【使用指南】使用公共的conda创建环境