Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I clear an image with Django Rest Framework?

I thought my problem was https://github.com/encode/django-rest-framework/issues/937 which should have been fixed by https://github.com/encode/django-rest-framework/pull/1003 but it appears, whether I send in None or empty string, DRF isn't happy.

I'm using Django 1.11.6 and DRF 3.7.7

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
like image 982
epalm Avatar asked Feb 08 '18 02:02

epalm


People also ask

Which is better Django or Django REST framework?

Django is a framework that is used for the backend part while the Django REST is used to render the database file in JSON or XML so that frontend can understand although Django self can do this thing but Django REST has many more features also. So it is good to use Django REST.

What is renderers in Django REST framework?

The rendering process takes the intermediate representation of template and context, and turns it into the final byte stream that can be served to the client. REST framework includes a number of built in Renderer classes, that allow you to return responses with various media types.

Is Django rest framework worth learning?

Django is an brilliant open source web framework which comes with its RESTful API's. You can go ahead and create attractive, easy to use API for your application and for that, you don't have to look anywhere except Django REST Framework!!

What is the advantage of Django REST framework?

Main advantages of Django REST framework: Simplicity, flexibility, quality, and test coverage of source code. Powerful serialization engine compatible with both ORM and non-ORM data sources. Pluggable and easy to customise emitters, parsers, validators and authenticators.


1 Answers

You have to specify the image field explicitly to allow it to be null.

use this:

class PartSerializer(serializers.ModelSerializer):
    image = serializers.ImageField(max_length=None, allow_empty_file=True, allow_null=True, required=False)

    class Meta:
        model = Part
        fields = ('id', 'image')

check docs for more details.

like image 181
zaphod100.10 Avatar answered Oct 04 '22 15:10

zaphod100.10