DRF提交的数据不是文件。检查表单上的编码类型 - ImageField

时间:2016-10-29 00:03:16

标签: json django serialization django-rest-framework

我有以下模型User序列化程序:

team字段中的ForeignKey字段为User,并且已对其进行序列化,以便使用setup_eager_loading函数优化其查询效果

class UserSerializer(serializers.ModelSerializer):
    team = serializers.StringRelatedField()

    def setup_eager_loading(queryset):
      queryset = queryset.select_related('team',)

    class Meta:
        model = User
        fields = ('url', 'username','password','first_name','last_name',
        'photo','team','position','last_login',)

我的viewsetUser如下:

from rest_framework import viewsets
from rest_framework import filters

from .models import User
from .serializers import UserSerializer



# Viewsets define the behavior of the view
class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    filter_fields = ('username','is_player', 'first_name','last_name','team','email',)

当我转到api序列化程序中的用户实例并希望通过raw data media type: application/json进行更新时,我收到以下消息:

enter image description here

在我的User模型中,图像字段以这种方式定义:

class User(AbstractBaseUser, PermissionsMixin):
#... other attributes  
    photo = models.ImageField(upload_to='avatars', blank=True)

如何通过JSON原始数据更新用户序列化模型中的任何字段/属性,而不会在photo字段中显示有关编码类型的消息

this question中讨论与更新请求中预期的DRF相关的base64编码字符串,但我尝试添加Base64ImageField序列化程序并在我的UserSerializer类中使用它,但我得到了这样:

enter image description here

除外:

Exception Value: Incorrect padding

0 个答案:

没有答案