在django rest框架中使用邮递员上传图像时出错

时间:2018-05-26 17:35:21

标签: python django django-rest-framework django-serializer

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

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)

这是Serializer,

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. 如何使用django rest framework正确上传图像。

  2.   
  3. 我不希望这个api一起调用4张图片,但一次只使用一张图片调用4次,如何传递相关名称和   相应地修改序列化程序。

  4.   
  5. 如何为根媒体目录提供子路径。

  6.   
  7. 最后我希望序列化程序显示完整的图像网址,我该怎么做?

  8.   

1 个答案:

答案 0 :(得分:1)

您使用的是serializer = UserMediaSerializer(request.data),但您应该通过serializer = UserMediaSerializer(data=request.data)

进行调用

要在Django休息框架中上传图像,您应该在S3上传图像并在DRF API中传递s3网址或在序列化程序中使用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')