当前位置:网站首页>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
直接区分开了,不同的请求方法调用不同序列
边栏推荐
- 厉害了!淮北两企业获准使用地理标志产品专用标志
- Debugging wechat built-in browser with chrome
- MySQL notes
- 数据的备份与恢复
- Importbeandefinitionregistrar registers beans with the container
- Take you to develop an efficiency enhancing tool -- vscode plug-in
- QML控件类型:SwipeView、PageIndicator
- Browser -- a collection of common search operators -- use / instance
- Common knowledge arrangement of numpy database
- 浏览器--常用的搜索操作符大全--使用/实例
猜你喜欢

Daemon flow

It is easy to analyze and improve R & D efficiency by understanding these five figures

JUC - thread interrupt and thread waiting and wakeup (locksupport)

Take you to develop an efficiency enhancing tool -- vscode plug-in

10 "no no no" redis interview questions

Zhongmin online: sharing online channel resources with "points" as the link
![[fault diagnosis] stitch Py script failure](/img/5c/e5df21674b5d0677484b49a608f8ae.png)
[fault diagnosis] stitch Py script failure

厉害了!淮北两企业获准使用地理标志产品专用标志

QML control types: swipeview, pageindicator

Pull down refresh, push up load (easy to use, finally)
随机推荐
[fault diagnosis] using multithreading, the program does not report an error but does not run
软件架构与模式:结构、组件、关系
Systematic arrangement | how many children's shoes have forgotten to be done carefully before the model development (practical operation)
【故障诊断】stitch.py脚本失效
Will swift compile to native code- Does Swift compile to native code?
【科研笔记】关于使用openslide切图的下采样倍数
Final examination questions of Database Principles
Remote Dictionary Server(Redis)——基于 KV 结构的作为 Cache 使用的 NoSQL 数据库管理系统
【SDX62】IPA log抓取操作说明
[scientific research notes] about using the downsampling multiple of openslide cut
软考2022年下半年考试科目安排
Disturbed when programmers are programming? Daily anecdotes
Zhongmin online: sharing online channel resources with "points" as the link
QT save qtextedit memory to Txt file
QML控件类型:SwipeView、PageIndicator
Qt保存QTextEdit内存至.txt文件中
What is the difference between TOC and Tob
The application of RPC technology and its framework sekiro in crawler reverse, encrypting data is a shuttle!
[fault diagnosis] CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace B
这是一个图片