3

I have the following model User serializer:

The team field is a ForeignKey in the User model and it's serialized for optimize their query in performance with the setup_eager_loading function

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',)

My viewset to User is the folowing:

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',)

When I go to an user instance in my api serializers and I want update via raw data media type: application/json, I get the following message:

enter image description here

In my User model the image field is defined of this way:

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

How to can I update any field/attribute in my User serialized model via JSON raw data without appear this message about of encoding type in the photo field

In this question talk about of base64 encoded string in relation to DRF expected in the update request, but I try adding the Base64ImageField serializer and using it in my UserSerializer class but I get this:

enter image description here

with the exception:

Exception Value: Incorrect padding
Community
  • 1
  • 1
bgarcial
  • 2,915
  • 10
  • 56
  • 123

0 Answers0