Django rest_fram_work API View序列化

APIview

单表的GET和POST:

视图

查询所有:

class PublishView(APIView):
    # 查询数据
    def get(self, request):
        # first inquire database
        publish = models.Publisher.objects.all()
        # data put serializers data packging
        bs = PublishModelSerializers(publish, many=True)  # many=True多个对象
        # return
        return Response(bs.data)

    # 增加数据
    def post(self, request):
        bs = PublishModelSerializers(data=request.data,many=True)  # post不需要定义many=Ture

        if bs.is_valid():
            bs.save()  # 保存
            return Response("添加成功")
        else:
            return Response("增加失败")

 

查询单条:

class PublishDetaiView(APIView):
    # 将这个pk设置成和lookup_url_kwarg='pk' 一样的值,不然加后缀会取不到值
    def get(self, request, id):  # id 不要放到request前面
        # 查询数据库
        publish = models.Publisher.objects.filter(pk=id)
        # 封装打包序列化数据
        bs = PublishModelSerializers(publish,many=True)  # many=True多个对象
        print(bs, 6666666666666666666666666)
        # Response 会直接返回josn数据格式
        ret = Response(bs.data)

        return ret

    # #  修改数据(前端指定id值后,在data中输入k:v即可change数据)
    def put(self, request, id):
        # inquire database
        publish = models.Publisher.objects.filter(pk=id).first()
        # data=  form request.data client
        ps = PublishModelSerializers(publish, data=request.data)
        # if ps pass verify
        if ps.is_valid():
            ps.save()
            return Response(ps.data)
        else:
            return Response(ps.errors)

    # 删除数据(功能还未实现)
    def delete(self, request, id):
        print(type(id),5555555555555)
        models.Publisher.objects.filter(id=id).delete()
        return Response("删除成功")

 

外键的GET和POST:

视图

查询所有:

# 多条数据
class BookView(APIView):
    def get(self, request, *args, **kwargs):
        book_list = models.Book.objects.all()
        # context十分关键,如果不将request传递给它,在序列化的时候,图片与文件这些Field不会再前面加上域名,也就是说,只会有/media/img...这样的路径!
        bs = BookModelSerializers(instance=book_list, many=True, context={'request': request})  # 在做链接的时候需要添加context参数
        print(bs)
        # 默认就返回json格式的字符串
        return Response(bs.data)

    # 增加数据
    def post(self, request):
        bs = BookModelSerializers(data=request.data)  # post不需要定义many=Ture
        if bs.is_valid():
            bs.save()  # 保存
            return Response("添加成功")
        else:
            return Response("增加失败")

 

查询单条:

# 单条数据
class BookDetaiView(APIView):
    # inquire database
    def get(self, request, id, *args, **kwargs):
        book_list = models.Book.objects.filter(id=id)
        bs = BookModelSerializers(instance=book_list, many=True, context={'request': request})  # 在做链接的时候需要添加context参数
        # 默认就返回json格式的字符串
        return Response(bs.data)

    def put(self, request, id, *args, **kwargs):
        book = models.Book.objects.filter(pk=id).first()
        bs = BookModelSerializers(book, data=request.data)  # 单条数据不能使用 many = Ture
        if bs.is_valid():
            bs.save()
            return Response("添加成功")
            # return Response(bs.data)  # 一般用这个
        else:
            return Response("添加失败")
            # return Response(bs.errors) # 一般用这个

    def delete(self, request, id):
        models.Book.objects.filter(pk=id).delete()
        return Response("删除成功")
posted @ 2019-12-18 00:59  Mr_Riven  阅读(235)  评论(0编辑  收藏  举报