上传文件接口

urls.py
from django.conf.urls import url, include
from . import views
from rest_framework.routers import SimpleRouter
router = SimpleRouter()

# /books/image/(pk) 提交 form-data:用image携带图片,这的image要与反序列字段的image一致
router.register('books/image', views.BookUpdateImageAPIView, 'book')

urlpatterns = [
    url('', include(router.urls))
]
serializers.py
class BookUpdateImageModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Book
        fields = ['image']
views.py
# 上传文件 - 修改头像 - 修改海报
from rest_framework.viewsets import GenericViewSet
from rest_framework import mixins
class BookUpdateImageAPIView(GenericViewSet, mixins.UpdateModelMixin):
    queryset = models.Book.objects.filter(is_delete=False).all()
    serializer_class = serializers.BookUpdateImageModelSerializer

 

posted @ 2020-02-23 21:27  Hank·Paul  阅读(604)  评论(0编辑  收藏  举报