class Part(models.Model):
image = models.ImageField(null=True, blank=True)
class PartSerializer(serializers.ModelSerializer):
class Meta:
model = Part
fields = ('id', 'image')
class PartDetail(generics.RetrieveUpdateAPIView):
queryset = Part.objects.all()
serializer_class = PartSerializer
parser_classes = (MultiPartParser, FormParser)
# put image, works fine
with tempfile.NamedTemporaryFile(suffix='.jpg') as fp:
image = Image.new('RGB', (100, 200))
image.save(fp)
fp.seek(0)
data = {'image': fp}
self.client.put('/path/to/endpoint', data, format='multipart')
# clear image, attempt #1
data = {'image': None}
self.client.put('/path/to/endpoint', data, format='multipart')
# AssertionError: {'image': ['The submitted data was not a file. Check the encoding type on the form.']}
# clear image, attempt #2
data = {'image': ''}
self.client.put('/path/to/endpoint', data, format='multipart')
# AssertionError: <ImageFieldFile: None> is not None