![c41fd963ba09631b3b1160a4a51b4b00.png](https://i-blog.csdnimg.cn/blog_migrate/a612c2015c007bc0fffdbab19be39bef.jpeg)
Ⅰ 基于前文封装一个简单的响应对象
models.py表模型
from django.db import models
# Create your models here.
class Book(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=32)
publish = models.CharField(max_length=32,null=True)
price = models.DecimalField(max_digits=6,decimal_places=2,null=True)
urls.py路由
from django.urls import path,re_path
from app01 import views
urlpatterns = [
path('books', views.Books.as_view()),
re_path('^books/(?P<id>d+)', views.Book.as_view()),
]
views.py视图函数
- from app01.utils import CommonResponse
from rest_framework.response import Response
from app01.serializer import BookSerializer
from app01 import models
from rest_framework.views import APIView
from app01.utils import CommonResponse
class Book(APIView):
def put(self,request,id):
res = CommonResponse()
post_res = request.data
book_res = models.Book.objects.all().filter(id=id).first()
ser = BookSerializer(instance=book_res,data=post_res) # instance是需要更新的对象实例
if ser.is_valid():
ser.save()
res.msg = '成功'
res.result = ['自定义数据','自定义数据']
else:
res.code = 404
res.msg = '未知错误'
return Response(res.get_dic)
serializer.py序列化器
class BookSerializer(serializers.Serializer):
id = serializers.IntegerField(required=False, ) # required设置后表示改字段可以不传
title = serializers.CharField(max_length=32, min_length=2, )
price = serializers.DecimalField(max_digits=6, decimal_places=2, )
publish = serializers.SerializerMethodField()
def update(self, instance, validated_data):
instance.title = validated_data.get('title')
instance.price = validated_data.get('price')
instance.publish = validated_data.get('publish')
instance.save()
return instance
tuils.py自定义封装响应对象
class CommonResponse():
def __init__(self):
self.code = 200
self.msg = ''
@property
def get_dic(self):
return self.__dict__
![3571fb63d86c8b3c2466b9d676071f15.png](https://i-blog.csdnimg.cn/blog_migrate/ebc54e1af35a4c849247d081652b165c.jpeg)
Ⅱ 通过配置修改响应的样式
配置文件方式 -- (全局)
- 如果没有配置,默认有浏览器和json两种样式
- drf有默认配置文件
- from rest_framework.settings import DEFAULTS -- 导入后,点进去,复制下面代码
![2dc5474e237ddea123f8562fee5e5c99.png](https://i-blog.csdnimg.cn/blog_migrate/71fdf870db81fb2db3304a2512747d18.jpeg)
我们需要在自己的配置文件中配置
项目根路径下的settings.py配置文件
# 所有drf的配置都在这个对应的字典里
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': ( # 默认响应渲染类
'rest_framework.renderers.JSONRenderer', # json渲染器
'rest_framework.renderers.BrowsableAPIRenderer', # 浏览API渲染器
)
}
只保留json渲染器
- 'rest_framework.renderers.JSONRenderer', # json渲染器
![7ca7aa49dd066099402c677f8a8a18ea.png](https://i-blog.csdnimg.cn/blog_migrate/3d56e70ee7a00575f34e42c5a9d116ca.jpeg)
只保留浏览器api渲染器
- 'rest_framework.renderers.BrowsableAPIRenderer', # 浏览API渲染器
![251ea76fb453b5317ae417da88563915.png](https://i-blog.csdnimg.cn/blog_migrate/3a17cec345c11dc0fc008afd6c14da42.jpeg)
在视图类中配置 -- 局部配置
- 力度更小,指定这个类的响应格式
- 导入JSONRenderer,并在视图类中写renderer_classes = [JSONRenderer,]即可
from rest_framework.renderers import JSONRenderer
class Book(APIView):
renderer_classes = [JSONRenderer,]
![76178235354004b24916658f127e1e0e.png](https://i-blog.csdnimg.cn/blog_migrate/469261d9a8ec76094819d9036fc8f567.jpeg)
- 局部与全局配置冲突,以局部为准
Ⅲ 修改响应配置的html样式
改他有什么意义呢?
![352efd7b1fc52dfdf793f45fe5d89fc6.png](https://i-blog.csdnimg.cn/blog_migrate/1f57553408e82a5687a34013584bafd3.jpeg)
![00c9eb42d31ee6b5c04c6eb2d95d8d70.png](https://i-blog.csdnimg.cn/blog_migrate/803971828f47ff3f9dd3be4f643e9482.jpeg)