title: How to post multiple images in a directory through the django rest framework in one click
body: <p>I am trying to use the django rest framework to carry out multiple POST events once I point it to a directory. The idea is that I will specify the country and a directory containing multiple images. Then the API must go to the directory, and post each image in that directory to the database along with more details available from the filename of each image. Following some pointers from the internet, I managed to build out a little model and framework that looks like it could work except for one thing. The default rest API page doesn't support the HTML input of lists. </p>
  
  <p>I would like to know if my time will be better spent trying to fit the API page to my requirements or if I should go about building my own HTML interface.</p>
  
  <p>Or to be more direct</p>
  
  <p>What is the fastest (and simplest) way for me to upload multiple images (each image is a separate instance in the db) into a database using Django and the Django REST Framework? </p>
  
  <p><strong>models.py:</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>from django.db import models
  
  
  # Create your models here.
  class Tiles(models.Model):
      image = models.ImageField(blank=True, null=True)
      lat = models.FloatField(blank=True, null=True)
      lng = models.FloatField(blank=True, null=True)
      country = models.CharField(blank=True, null=True, max_length=10)
      solar = models.FloatField(blank=True, null=True)
      wind = models.FloatField(blank=True, null=True)
      HV = models.FloatField(blank=True, null=True)
  
      class Meta:
          verbose_name_plural = "Tiles"
  
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>from django.shortcuts import render
  from rest_framework import viewsets
  from .serializers import FileListSerializer
  from rest_framework import parsers
  from .models import Tiles
  
  
  # Create your views here.
  def index(request):
      return render(request, 'main/index.html')
  
  
  class TileViewSet(viewsets.ModelViewSet):
      serializer_class = FileListSerializer
      parser_classes = (parsers.MultiPartParser, parsers.FormParser,)
      queryset = Tiles.objects.all()
  </code></pre>
  
  <p><strong>serializers.py</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>from rest_framework import serializers
  from .models import Tiles
  import mercantile
  
  
  class FileListSerializer(serializers.Serializer):
      image = serializers.ListField(
                         child=serializers.FileField(max_length=100000,
                                           allow_empty_file=False,
                                           use_url=False)
                                  )
      country = serializers.CharField(max_length=10)
  
      # The create function below overrides the Django default create function to populate multiple fields using the image name
  
      def create(self, validated_data):
  
          country = validated_data.pop('country')
          image = validated_data.pop('image')
          for img in image:
  
              path = img.path
              path = path.split('/')[-1]
              path = path.split('.')[0]
              x, y, z = path.split('-')
              coords = mercantile.ul(int(x), int(y), int(z))
  
              tile = Tiles.objects.create(image=img, country=country, lat=coords.lat, lng=coords.lng, **validated_data)
  
          return tile
  
  
  class TileSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = Tiles
          fields = '__all__'
  
  </code></pre>
  
  <p>Screenshot of the resulting API page:
  <a href="https://i.stack.imgur.com/fJm7L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fJm7L.png" alt="Django rest framework autogenerated API page"></a></p>