def patch(self, request, format=None): # 更新分类 category = CategorySerializers(Category, data=request.data, partial=True) if category.is_valid(): # print(category.validated_data) category.save() json_data = {"msg": "修改成功", "code": 1} else: json_data = {"msg": "修改失败", "code": 0} return Response(json_data)
def post(self, request, format=None): # 创建分类 # 反序列化,将json转化成model category = CategorySerializers(data=request.data) if category.is_valid(): # 验证分类名重复性 if Category.objects.filter( category_name=category.validated_data['category_name'] ).first() is None: category.save() json_data = {"msg": "添加成功", "code": 1} else: json_data = {"msg": "已存在该分类", "code": 0} else: # print(category.errors) json_data = {"msg": "添加失败", "code": 0} return Response(json_data)