我有型号产品:
class Product(models.Model):
...
image = models.ImageField(
upload_to=lambda instance, filename: 'images/{0}/{1}'.format(instance.id, filename),
max_length=254, blank=True, null=True
)
...
然后我有序列化器:
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = (
...
'image',
...
)
然后我有意见:
class ProductViewSet(BaseViewSet, viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
@detail_route(methods=['POST'])
def upload_image(self, request, *args, **kwargs):
upload = request.data['file']
print(upload.name)
product = self.get_object()
product.image.delete()
upload = request.data['file']
product.image.save(upload.name, upload)
return Response({'Location': product.image.url}, status=status.HTTP_201_CREATED)
问题仅出在图像上。在浏览器黑色窗口中上传图片更改'源代码',我无法打开它。 Mp3和PDF格式工作正常。为什么会这样?谢谢。
答案 0 :(得分:1)
也许问题出在base64图片上?在这种情况下,您应该从drf_base64.serializers导入ModelSerializer并从中继承。