我正在使用 django rest 框架 django rest swagger 创建简单的 swagger 文档。
Python: 3.6
Django: 3.0.3
django-rest-swagger: 2.2.0
djangorestframework: 3.11.0
这是模型.py
class Image(models.Model):
file = models.ImageField(upload_to='photos', null=True, blank=True)
is_active = models.BooleanField(default=False, null=True, blank=True)
created_at = models.DateTimeField(default=timezone.now)
class Meta:
db_table = 'image'
def __str__(self):
return self.file.name
我创建了serializers.py
class ImageSerializer(serializers.ModelSerializer):
class Meta:
model = Image
fields = ('id', 'file', 'created_at')
这里还有image_view.py
from rest_framework import generics
from rest_framework.parsers import FormParser, MultiPartParser, FileUploadParser
from api.serializers import ImageSerializer
from api.models import Image
class ImageView(generics.CreateAPIView):
serializer_class = ImageSerializer
parser_classes = (FileUploadParser, )
def post(self, request, *args, **kwargs):
"""
Image Moodel
---
parameters:
- name: file
type: file
responseMessages:
- code: 201
message: Created
"""
file = request.data.get('file')
image = Image(file=file, name=file.name)
return self.create(request, *args, **kwargs)
图片上传显示为字符串。
如何解决这个问题?