gpt4 book ai didi

python - 在djangorest框架中使用postman上传图像时出错

转载 作者:行者123 更新时间:2023-12-01 09:24:31 27 4
gpt4 key购买 nike

我正在尝试创建一个端点,以使用 django Rest 框架将图像(使用 postman )上传到特定文件夹。这是我对文件夹的设置,

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

这是我的模型,

class UserMedia(models.Model):
user = models.OneToOneField(User, related_name='medias', on_delete=models.CASCADE, )
profile_image_web = models.FileField(null=True)
profile_image_android = models.FileField(null=True)
profile_image_ios = models.FileField(null=True)
thumbnail = models.FileField(null=True)

这是序列化器,

class UserMediaSerializer(serializers.ModelSerializer):
class Meta:
model = models.UserMedia
fields = (
'profile_image_web', 'profile_image_ios', 'profile_image_android', 'thumbnail',
)

这是API,

class CreateUpdateUserMedia(views.APIView):
parser_classes = (MultiPartParser, FormParser)

def post(self, request, **kwargs):
serializer = UserMediaSerializer(request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

现在,当我尝试使用 POSTMAN 上传与其中一个字段对应的一张图像时,这是我收到的错误。

'Cannot call `.is_valid()` as no `data=` keyword argument was '
AssertionError: Cannot call `.is_valid()` as no `data=` keyword argument was passed when instantiating the serializer instance.

这是完全可以理解的,但我不知道如何解决它。

这是我的问题,

  1. How do I correctly upload images using django rest framework.

  2. I don't expect this api to be called with 4 images together, but 4 times using one image at a time, how do I pass the relevant name and modify the serializer accordingly.

  3. How do I provide a subpath to the root media directory.

  4. Finally I want the serializer to display the full image url, how do I do that?

最佳答案

您正在使用serializer = UserMediaSerializer(request.data),但您应该通过serializer = UserMediaSerializer(data=request.data)调用它

要在 Django Rest 框架中上传图像,您应该在 S3 上上传图像并在 DRF API 中传递 s3 url,或者在序列化程序中使用 Base64 字段并在 API 中发送图像的 Base64 编码值

import uuid
import base64
import imghdr

from django.utils.translation import ugettext_lazy as _
from django.core.files.base import ContentFile
from rest_framework import serializers


ALLOWED_IMAGE_TYPES = (
"jpeg",
"jpg",
"png",
"gif"
)


class Base64ImageField(serializers.ImageField):
"""
A django-rest-framework field for handling image-uploads through raw post data.
It uses base64 for en-/decoding the contents of the file.
"""

def to_internal_value(self, base64_data):
# Check if this is a base64 string
if not base64_data:
return None

if isinstance(base64_data, basestring):
# Try to decode the file. Return validation error if it fails.
try:
decoded_file = base64.b64decode(base64_data)
except TypeError:
raise serializers.ValidationError(_("Please upload a valid image."))
# Generate file name:
file_name = str(uuid.uuid4())[:12] # 12 characters are more than enough.
# Get the file name extension:
file_extension = self.get_file_extension(file_name, decoded_file)
if file_extension not in ALLOWED_IMAGE_TYPES:
raise serializers.ValidationError(_("The type of the image couldn't been determined."))
complete_file_name = file_name + "." + file_extension
data = ContentFile(decoded_file, name=complete_file_name)
return super(Base64ImageField, self).to_internal_value(data)
raise serializers.ValidationError('This is not an base64 string')

def to_representation(self, value):
# Return url including domain name.
return value.name

def get_file_extension(self, filename, decoded_file):
extension = imghdr.what(filename, decoded_file)
extension = "jpg" if extension == "jpeg" else extension
return extension

已更新

您应该对图像使用 ImageField(而不是 FileField)。

您可以像任何其他字段一样直接在序列化器中使用上述字段。

class UserMediaSerializer(serializers.ModelSerializer):
profile_image_web = Base64ImageField(required=False)
class Meta:
model = models.UserMedia
fields = ('profile_image_web', 'profile_image_ios', 'profile_image_android', 'thumbnail')

关于python - 在djangorest框架中使用postman上传图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50545408/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com