我有以下模型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',)
我的viewset
到User
如下:
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
进行更新时,我收到以下消息:
在我的User
模型中,图像字段以这种方式定义:
class User(AbstractBaseUser, PermissionsMixin):
#... other attributes
photo = models.ImageField(upload_to='avatars', blank=True)
如何通过JSON原始数据更新用户序列化模型中的任何字段/属性,而不会在photo
字段中显示有关编码类型的消息
在this question中讨论与更新请求中预期的DRF相关的base64编码字符串,但我尝试添加Base64ImageField
序列化程序并在我的UserSerializer
类中使用它,但我得到了这样:
除外:
Exception Value: Incorrect padding