目标:不用写序列化器,把前端传到浏览器的数据保存下来
***方法:request.data.get(‘字段名’***)
代码如下:以django用户评论为例
class CommentView(APIView):
#从前端浏览器可以看到前端请求的数据
# comment: “案二审下单错天赋vygbuhj”
# is_anonymous: false
# order: “20190214191558000000002”
# score: 5
# sku: 3
permission_classes = [IsAuthenticated]
# def update(self):
# return OrderInfo.objects.filter(order_id=self.kwargs.get(‘order_id’))[0].skus.all().filter(is_commented__isnull=False)
# serializer_class = CommentSerializer
def post(self,request,pk):
# 获取参数
comment=request.data.get('comment')
is_anonymous=request.data.get('is_anonymous')
order=request.data.get('order')
score=request.data.get('score')
sku=request.data.get('sku')
# print(score)
# print(comment)
order_goods = OrderGoods.objects.get(order_id=order,sku_id=sku)
order_goods.comment=comment
order_goods.is_anonymous=is_anonymous
order_goods.score=score
order_goods.save()
a=Response({
'comment':comment,
'is_anonymous':is_anonymous,
'order':order,
'score':score,
'sku':sku
})
return a
注意:post里面的pk可以不写,我写的原因是前端定义好了url地址,那是请求参数,不写会报错。