I have the following model User:
class User(AbstractBaseUser, PermissionsMixin):
#... other attributes
photo = models.ImageField(upload_to='avatars', blank=True)
The basic serialization that I made of this model is:
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',)
The team
field is a foreign key in the User model, which is serialized to optimize its performance in queries to the database when there are relations, with the setup_eager_loading function
method.
The viewset function of the User
model is:
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 instance of a user in my serialized api via Django Rest Framework, and I want to update some data (any) of that instance through pure json data (json raw data media type: application / json) I get this message:
How can I manipulate at the CRUD level the User
objects serialized in my api through pure json data in Django Rest Framework without having coding errors?