Uploading Images — Django

Build a Product Review Backend with DRF — Part 5

Emre Cevik
Python | Django & Rest
4 min readOct 17, 2020

--

We’ll use django-versatileimagefield package for uploading images.

A drop-in replacement for django’s ImageField that provides a flexible, intuitive and easily-extensible interface for creating new images from the one assigned to the field.

First we need to install django-versatileimagefield and python-magic-bin package

pip install django-versatileimagefield
pip install python-magic-bin==0.4.14

After installation completes, add versatileimagefield to INSTALLED_APPS:

We’ll store images in a Image Model and define M2M relation with Product Model.

Let’s create Image Model. Open reviews/models.py and type the following lines of code:

VersatileImageField provides a simple, flexible interface for creating new images from the image you assign to it. Each image managed by a VersatileImageField can store its own, unique PPOI (Primary Point of Interest) in the database via the easy-to-use PPOIField. For Integrate it into model you’ll need to add a new PPOIField field to your model and then include the name of that field in the VersatileImageField’s ppoi_field keyword argument.

For adding Image model to the administration site open the file reviews/admin.py and type the following lines of code:

We need to define relation between Product and Image model.

In debug mode, Django will serve everything, so that you don’t have to bother with anything else. With debug turned off Django won’t handle media files for you any more. Your production web server (Apache, Nginx etc.) should take care of that.

Django development server doesn’t serve media files by default. We need to add MEDIA_URL and MEDIA_ROOT. Open medium/settings.py file and type the following lines of code:

To make Django development server serve media we need to add a URL pattern in urls.py file. Open medium/urls.py file and type the following lines of code:

Run makemigrations command to create migration file for changes made to models.py:

$ python manage.py makemigrations

Run migrate command for synchronizing the changes you made to your models with the schema in the database.

$ python manage.py migrate

Run server and check admin site. You should see Image model on the Reviews Section.

After this settings add product page should look like below:

Add some images to database and attach to product via site admin

Let’s create our image serializer. Open reviews/serializers.py file and type the following lines of code:

The sizes argument on VersatileImageFieldSerializer simply unpacks the list of 2-tuples using the value in the first position as the attribute of the image and the second position as a ‘Rendition Key’ which dictates how the original image should be modified.

Now we can change product serializer. Open reviews/serializers.py file and change ProductSerializer for image relation. When we call ?expand=image our images will be added to response.

Navigate to product/1/?expand=image&omit=content. You should see images in serialized data.

http://127.0.0.1:8000/product/1/?expand=image&omit=content

It’s common to want to re-use similar sets of images across models and fields so django-versatileimagefield provides a setting, VERSATILEIMAGEFIELD_RENDITION_KEY_SETS for defining them .

Let’s move the Rendition Key Set we used above into our settings file:

And we need to add product_headshot to our serializer. Open reviews/serializers.py file and change ImageSerializer:

Navigate to product/1/?expand=image&omit=content. You should see images in serialized data.

http://127.0.0.1:8000/product/1/?expand=image&omit=content

Detailed descriptions for versatileimagefield you can check

Uploading image via api

First we need to create view action for post. Open reviews/views.py file and type the following lines of code:

FlexFieldsModelViewset is subclass of ModelViewset and provides create(), retrieve(), update(), partial_update(), destroy() and list() actions automaticaly.

Using Postman, we can create post request for an image endpoint:

When a client sends request to our API for an image endpoint using POST request, API call ImageViewSet create() action and serializer/view response image instance or error code.

Navigate to images on admin site. You should see “My Image Name”.

You can download part 5 files from

If you want to learn more about Django, do check out the documentation, django rest framework website and make sure to check out parts of this series!

--

--