1

I have a view in DRF - when I select the image then it works fine but when no files are selected then it throws an error:

The submitted data was not a file. Check the encoding type on the form.code invalid

I have also tried conditionally putting the imagefield i.e. (mainimage and image_with_self) in the dat1 {}, but that does not work as well. It does not include the key-value-pair in the dat1={} instead it put outside the dictionary object as old[Inmemoryuploaded:filename]
Below is my code

class ArtworkViewSet(viewsets.ModelViewSet):
    
    queryset = Artwork.objects.all()
    # import pdb; pdb.set_trace();
    serializer_class = ArtworkSerializer
    permission_classes = [AllowAny]
    
    def partial_update(self, request, *args, **kwargs):
        # import pdb; pdb.set_trace()
        # kwargs['partial'] = True
        partial = kwargs.pop('partial', True)
        instance = self.get_object()
        dat1={}
        if request.data.get('own_work') == "false":
            name_of_creator=request.data.get('name_of_creator')
            name_of_owner=request.data.get('name_of_owner')
            age_of_work=request.data.get('age_of_work')
            house_no=request.data.get('house_no')
            building_name=request.data.get('building_name')
            
        else:

            user_id=request.user.id
            userprofile = UserProfile.objects.get(user=user_id)
            name_of_creator=userprofile.name
            name_of_owner=userprofile.name
            house_no=userprofile.house_no
            building_name=userprofile.building_name
            
      
        # if(request.data['image_with_self']!= ""):
       
        #     dat1['image_with_self']=request.data['image_with_self']

        # if(request.data['mainimage'] != ""):
        
        #     dat1['mainimage']=request.data['mainimage']
       
        

        


        dat1={  

            'title': request.data.get("title"),
           
           'description': request.data.get("description"),
           'artcontest': request.data.get("artcontest"),


            'mainimage':request.data['mainimage'],
            'image_with_self':request.data['image_with_self'],


           'category_id': request.data.get("category"),
           'size': request.data.get("size"),
           'weight': request.data.get("weight"),
           
           'material': request.data.get("material"),
           'sale_lend': request.data.get("sale_lend"),
           'own_work': request.data.get("own_work"),
           'name_of_creator': name_of_creator,
           'name_of_owner': name_of_owner,
           'house_no': house_no,
           'building_name': building_name,
             
            }
        # instance.save()

        import pdb; pdb.set_trace()
         
        serializer= self.get_serializer(instance, dat1, partial=partial)
       
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)
   
toydarian
  • 4,246
  • 5
  • 23
  • 35
manish singh
  • 87
  • 1
  • 13

1 Answers1

0

Did you make the image field optional in the serialize class. if not than may be you should.

image = serializers.ImageField(required=False, max_length=None, allow_empty_file=True, use_url=True)

I faced kind of similar error few day back. May be it can help you.

Fahad Md Kamal
  • 243
  • 6
  • 20
  • I am using ModelSerializer, and in model it is already blank=True, null=True. Also while updating the image field when nothing is being uploaded it should not replace the old file value in database. Do you know any way if I can define/override the required=False optionally in serializer? – manish singh Aug 12 '20 at 03:09
  • For File Field it not enough to set null and blank to true. You will have to make allow_empty_fill=True. .... For keeping previous image if nothing is passed you need to check if the image field is empty from the sterilizer's validator function. – Fahad Md Kamal Aug 12 '20 at 12:41