标签:from ide end class deletion 入库 辅助 spec headers
目录
经过前面的学习我们可以发现, 我们每次成功响应了前端的请求, 都要按照固定的格式写一遍Response内部包含的数据,并将其返回给前端, 每返回一次就要完整的写一次, 是不是有点麻烦?
我们可以通过对Response进行简单封装, 来简化我们的代码
# 封装前
return Response({
'status': 0,
'msg': 'ok',
'results': serializer_obj.data
})
# 封装后
return APIResponse(results=serializer_obj.data)
# 在app文件下新建一个response.py文件
from rest_framework.response import Response
# 定义APIResponse继承Response
class APIResponse(Response):
# 重写__init__方法
def __init__(self, status=0, msg='ok', results=None, http_status=None,
headers=None, exception=False, content_type=None, **kwargs):
# 将status, msg, results, kwargs放到data当中
data = {
'status': status,
'msg': msg
}
if results is not None:
data['results'] = results
data.update(**kwargs)
# 调用父类Response的__init__方法
super().__init__(data=data, status=http_status, headers=headers, exception=exception, content_type=content_type)
class PressModerSerializer(serializers.ModelSerializer):
class Meta:
model = models.Press
fields = ['name', 'addr', 'books']
# 设置查询深度为1
depth = 1
-----------------------------------------------------------------------------------------------------
{
"status": 0,
"msg": "ok",
"results": [
{
"name": "东方出版社",
"addr": "上海",
# 不设置查询深度, 显示的是主键值; 设置查询深度, 显示是主键值对应的记录
"books": [
{
"id": 1,
"is_delete": false,
"created_time": "2019-12-26T18:40:09",
"name": "三体",
"price": "49.90",
# 如果设置depth=2, 那下面id=1的press也会被查出来
"press": 1,
"authors": [
1
]
},
{
"id": 3,
"is_delete": false,
"created_time": "2019-12-26T18:42:08",
"name": "球状闪电",
"price": "36.60",
"press": 1,
"authors": [
1
]
}
]
},
# models.py
class Book(Base):
name = models.CharField(max_length=64)
price = models.DecimalField(max_digits=6, decimal_places=2)
press = models.ForeignKey(to='Press', related_name='books', db_constraint=False, on_delete=models.SET_NULL,
null=True)
authors = models.ManyToManyField(to='Author', related_name='books', db_constraint=False)
# 外键字段只对应一条数据时
@property
def book_press(self):
# 这里也可以利用序列化类
# from serializers import PressModelSerializer
# return PressModerSerializer(self.press).data
return {
'name': self.press.name,
'addr': self.press.addr
}
# 外键字段对应多条数据时
@property
def book_authors(self):
authors = []
for author in self.authors.all():
author_dic = {
'name': author.name,
}
# 如果作者没有详情, 进行异常捕获
try:
author_dic['mobile'] = author.detail.moblie
except:
author_dic['mobile'] = '无'
authors.append(author_dic)
return authors
-----------------------------------------------------------------------------------------------------
# serializers.py
class BookMethodSerializer(serializers.ModelSerializer):
class Meta:
model = models.Book
# 将自定义的@property属性方法在fields中配置
fields = ['name', 'price', 'book_press', 'book_authors']
pks = [pk, ]
# 单删群删
def delete(self, request, *args, **kwargs):
"""
单删
接口: /book/(pk)/, 数据: 空
群删
接口: /book/, 数据: [pk1, pk2, ...]
"""
pk = kwargs.get('pk')
if pk:
# 将单删变为群删一条数据
pks = [pk, ]
else:
pks = request.data
# 数据有误, 数据库执行会报错
try:
rows = models.Book.objects.filter(is_delete=False, pk__in=pks).update(is_delete=True)
except:
return APIResponse(1, 'invalid data', http_status=400)
if rows:
return APIResponse(0, 'delete ok')
else:
return APIResponse(1, 'delete fail')
/book/
# 单增群增
def post(self, request, *args, **kwargs):
"""
单增
接口: /book/, 数据: {...}
群增
接口: /book/, 数据: [{},{}, ...]
"""
if isinstance(request.data, dict):
is_many = False
elif isinstance(request.data, list):
is_many = True
else:
return APIResponse(0, 'invalid data', http_status=400)
serializer_obj = serializers.BookModelSerializer(data=request.data, many=is_many)
serializer_obj.is_valid(raise_exception=True)
book_obj_or_list = serializer_obj.save()
return APIResponse(results=serializers.BookModelSerializer(book_obj_or_list, many=is_many).data)
# ModelSerializer下面的create的方法只能实现单增
def create(self, validated_data):
(...)
return instance
# ListSerializer下面的create方法
def create(self, validated_data):
return [
# 对群增数据进行遍历, 遍历一个, 就调用ModelSerializer的create方法来增加一个
# self.child就是ModelSerializer对象
self.child.create(attrs) for attrs in validated_data
]
# ListSerializer的update方法
def update(self, instance, validated_data):
raise NotImplementedError(
"Serializers with many=True do not support multiple update by "
"default, only multiple create. For updates it is unclear how to "
"deal with insertions and deletions. If you need to support "
"multiple update, use a `ListSerializer` class and override "
"`.update()` so you can specify the behavior exactly."
)
# views.py
# 整体单改群改
def put(self, request, *args, **kwargs):
"""
单改
接口: /book/(pk)/, 数据: {...}
群改
接口: /book/, 数据: [{'pk':1,..},{'pk':2,..}, ...]
"""
pk = kwargs.get('pk')
# 单改
if pk:
try:
# 注意这里我们使用get方法, 没有的话就报错, 进行异常处理
book_instance = models.Book.objects.get(is_delete=False, pk=pk)
except:
return APIResponse(1, 'invalid data', http_status=400)
serializer_obj = serializers.BookModelSerializer(instance=book_instance, data=request.data)
serializer_obj.is_valid(raise_exception=True)
book_obj = serializer_obj.save()
return APIResponse(results=serializers.BookModelSerializer(book_obj).data)
# 群改
else:
try:
pks = []
for dic in request.data:
pk = dic.pop('pk')
pks.append(pk)
book_query = models.Book.objects.filter(is_delete=False, pk__in=pks).all()
if not len(book_query) == len(book_query):
raise Exception('pk error')
except Exception as e:
return APIResponse(1, msg=f'{e} error', http_status=400)
serializer_obj = serializers.BookModelSerializer(instance=book_query, data=request.data, many=True)
serializer_obj.is_valid(raise_exception=True)
book_list = serializer_obj.save()
return APIResponse(results=serializers.BookModelSerializer(book_list, many=True).data)
# serializers.py
# 自定义ListSerializer子类实现群改操作
class BookListSerializer(serializers.ListSerializer):
def update(self, instance_list, validated_data_list):
return [
self.child.update(instance_list[index], attrs) for index, attrs in enumerate(validated_data_list)
]
class BookModelSerializer(serializers.ModelSerializer):
class Meta:
# 配置自定义的ListSerializer类
list_serializer_class = BookListSerializer
model = models.Book
fields = ['name', 'price', 'book_press', 'book_authors', 'press', 'authors']
extra_kwargs = {
'press': {
'write_only': True
},
'authors': {
'write_only': True
}
}
partial=True
参数就行
# views.py
# 局部单改群改
def put(self, request, *args, **kwargs):
"""
单改
接口: /book/(pk)/, 数据: {...}
群改
接口: /book/, 数据: [{'pk':1,..},{'pk':2,..}, ...]
"""
pk = kwargs.get('pk')
# 单改
if pk:
try:
# 注意这里我们使用get方法, 没有的话就报错, 进行异常处理
book_instance = models.Book.objects.get(is_delete=False, pk=pk)
except:
return APIResponse(1, 'invalid data', http_status=400)
# 实例化serializer对象时添加partial=True
serializer_obj = serializers.BookModelSerializer(instance=book_instance, data=request.data, partial=True)
serializer_obj.is_valid(raise_exception=True)
book_obj = serializer_obj.save()
return APIResponse(results=serializers.BookModelSerializer(book_obj).data)
# 群改
else:
try:
pks = []
for dic in request.data:
pk = dic.pop('pk')
pks.append(pk)
book_query = models.Book.objects.filter(is_delete=False, pk__in=pks).all()
if not len(book_query) == len(book_query):
raise Exception('pk error')
except Exception as e:
return APIResponse(1, msg=f'{e} error', http_status=400)
# 实例化serializer对象时添加partial=True
serializer_obj = serializers.BookModelSerializer(instance=book_query, data=request.data, many=True, partial=True)
serializer_obj.is_valid(raise_exception=True)
book_list = serializer_obj.save()
return APIResponse(results=serializers.BookModelSerializer(book_list, many=True).data)
标签:from ide end class deletion 入库 辅助 spec headers
原文地址:https://www.cnblogs.com/bigb/p/12111795.html