I would like to have a model for which you can upload multiple images on create(post).
In DRF web view on api/animals/ in the post form I would like to add multiple images and create the new Animal with the images attached.
Let us assume I have the following models:
class Animal(models.Model):
slug = models.CharField(max_length=20, unique=True)
class AnimalImage(models.Model):
animal = models.ForeignKey(Animal, on_delete=models.CASCADE)
image = models.ImageField(upload_to='animal_pics/')
The I have the following serializers:
class AnimalImageSerializer(serializers.ModelSerializer):
class Meta:
model = AnimalImage
fields = ('animal', 'image', )
class AnimalSerializer(serializers.HyperlinkedModelSerializer):
images = AnimalImageSerializer(many=True)
class Meta:
model = Animal
lookup_field = 'slug'
extra_kwargs = {
{'url': {'lookup_field': 'slug'}
}
fields = ('slug', 'images', )
The I have the following rest views:
class AnimalViewSet(viewsets.ModelViewSet):
queryset = Animal.objects.all()
serializer_class = AnimalSerializer
lookup_filed = 'slug'
parser_classes = (JSONParser, MultiPartParser, FormParser)
When I use the the drf web interface: