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)