Problem when uploading image - Django rest framework

0

I have 2 models User and EcoUser with a relation of 1 to 1 (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 in the register I do not save images 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 I added the ImageField field and it did not update my data and I got a 400 bad request error. This I send to you from VUEJS for 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);
  });

It's okay if I put user.picture_url in the apppend as a field name? since it is inside the user object and then I already access the picture_url to be able to update it.

    
asked by Piero Pajares 28.07.2018 в 19:18
source

1 answer

0

Probe of the postman and I realized the error: It simply was that the username field of the django user table was mandatory:

const bodyFormData = new FormData();
bodyFormData.append('user.picture_url', this.params.user.picture_url.name);
bodyFormData.append('document', this.params.document);
bodyFormData.append('user.username', this.params.username);

and it worked wonders. : D

    
answered by 28.07.2018 / 23:59
source