当前位置:   article > 正文

rest_framework中的五大接口_restframework写post接口

restframework写post接口

rest_framework中有五大基础接口:get_list(获得多条数据),get_one(获得一条数据),post_one(添加一条数据),put_one(修改一条数据),delete_one(删除一条数据)

五大接口基础实现

from rest_framework.views import APIView
from rest_framework.response import Response
from app import models, objson
class Books(APIView):
    # 请求地址:/books/
    def get(self, request, *args, **kwargs):
        book_list = models.Book.objects.all()
        book_data = objson.BookJson(book_list, many=True).data
        return Response({
   
            'status': 0,
            'msg': 'ok',
            'results': book_data
        })
	
    # 请求地址:/books/  参数:{"name": "红楼梦","price": "66.66","author": [1]}
    def post(self, request, *args, **kwargs):
        book_json = objson.BookJson(data=request.data)
        if book_json.is_valid():
            book_json.save()
            return Response({
   
                'status': 0,
                
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/273006
推荐阅读
相关标签
  

闽ICP备14008679号