标签:维护 upd field 开发 mod 接口 并且 lte sim
在开发中,一般为上传文件单独设立一个接口,以降低程序的耦合度,并且更方便后期的维护。
from django.conf.urls import url, include
from . import views
# 路由组件,必须配合视图集使用
from rest_framework.routers import SimpleRouter
router = SimpleRouter()
# /books/image/(pk) 提交 form-data:用image携带图片
router.register('books/image', views.BookUpdateImageAPIView, 'book')
urlpatterns = [
url('', include(router.urls))
]
class BookUpdateImageModelSerializer(serializers.ModelSerializer):
class Meta:
model = models.Book
fields = ['image']
# 上传文件 - 修改头像
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
标签:维护 upd field 开发 mod 接口 并且 lte sim
原文地址:https://www.cnblogs.com/Ghostant/p/12348236.html