I have 2 User and EcoUser models with a 1 to 1 relationship (I have reduced the fields of the tables for this example):
class User(AbstractUser):
picture_url = models.ImageField(upload_to='logos/', blank=True)
class EcoUser(models.Model):
user = models.OneToOneField(User, related_name='eco_user')
document = models.CharField(max_length=45, blank=True)
def __str__(self):
return str(self.user)
In which I use a NestedSerializer to be able to create and update the data of the two tables in a single post or put , in this way I did the update since I do not save images in the registry and I have no problem with it:
This is the serializer
class EcoUserSerializer(serializers.ModelSerializer):
user = UserSerializer(required=True)
class Meta:
model = EcoUser
fields = '__all__'
def update(self, instance, validated_data):
instance.document = validated_data.get('document', instance.document)
instance.save()
user_data = validated_data.pop('user')
user = instance.user
user.picture_url = user_data.get('picture_url', user.picture_url)
user.save()
return instance
and in my viewset
class EcoUserViewSet(viewsets.ModelViewSet):
serializer_class = EcoUserSerializer
queryset = EcoUser.objects.all()
pagination_class = None
parser_classes = (MultiPartParser,)
@transaction.atomic
def update(self, request, *args, **kwargs):
with transaction.atomic():
try:
instance = self.get_object()
instance.id = kwargs.get('pk')
serializer = EcoUserSerializer(instance=instance, data=request.data)
print(serializer)
if serializer.is_valid(raise_exception=True):
self.perform_update(serializer)
return Response({"status": True, "results": "Datos actualizados correctamente"},
status=status.HTTP_201_CREATED)
except ValidationError as err:
return Response({"status": False, "error_description": err.detail}, status=status.HTTP_400_BAD_REQUEST)
This worked correctly until when I added the ImageField field, it no longer updated the data and gave me a 400 bad request error. This I send from VUEJS by axios :
const bodyFormData = new FormData();
bodyFormData.append('user.picture_url', this.params.user.picture_url.name);
bodyFormData.append('document', this.params.document);
this.axios.put(`/users/${this.params.id}/`, bodyFormData, { headers: { 'Content-Type': 'multipart/form-data' } })
.then((response) => {
this.isSending = false;
this.$snackbar.open(response.data.results);
});
Is it ok if in the apppend as field name I put user.picture_url ? since it is inside the user object and then I access the picture_url to be able to update it.
I tried postman and realized the error: It was simply that the username field of the django user table was required:
and it worked great. :D