I have a django rest api app. And I have a function for uploading multiple images in the admin of django. And everything works fine: a user can upload multiple images and also the images are stored in the database table
DierenWelzijnAdmin_animalimage
But if I access the api call: http://127.0.0.1:8000/api/animals/
I see an emtpy array returned:
{
"id": 30,
"images": "https://dier.blob.core.windows.net/media/media/photos/animals/amphibiance_X04Eh6N_1z0N4Gs_9EPmeMA_6j3axdY_da4cpTO_2VAY67x_hcUVB7f.jpg",
"animal_images": []
},
So this is the model:
import sys
from io import BytesIO
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.contrib.auth.models import Permission
from PIL import Image
from django.db import models
from django.conf import settings
class AnimalImage(models.Model):
animal = models.ForeignKey(
'Animal', related_name='imag', on_delete=models.CASCADE)
image = models.ImageField(upload_to='media/photos/animals')
def __str__(self):
return f"Image for {self.animal.name}"
class Animal(models.Model):
images = models.ImageField(
upload_to="media/photos/animals", blank=False, null=False, verbose_name="Foto")
animal_images = models.ManyToManyField(
AnimalImage, related_name='related_animals', blank=True)
def img_preview(self): # new
return mark_safe(f'<img src = "{self.images.url}" width = "300"/>')
img_preview.short_description = "Huidige Foto"
def img_previews(self):
images = self.images.all()
return mark_safe(''.join([f'<img src="{img.image.url}" width="100"/>' for img in images]))
img_previews.short_description = "Thumbnails"
class Meta:
verbose_name = "Dier"
verbose_name_plural = "Dieren"
# permissions = [("set_display_flag", "Check name is display or not",)]
super(Animal, self).save()
def __str__(self):
return self.name
admin.py file:
from django.contrib import admin
from django.db import models
from django.forms import RadioSelect
from django.http import HttpResponseRedirect
from django.urls import reverse
from .models import Animal, AnimalImage
from .forms import SetCategoryForm
from django.utils.safestring import mark_safe
class AnimalImageInline(admin.TabularInline):
model = AnimalImage
extra = 1
class AnimalAdmin(admin.ModelAdmin):
inlines = [AnimalImageInline]
fields = ['name', 'images', 'animal_images', 'img_preview']
readonly_fields = ['img_preview', 'klasse_name']
autocomplete_fields = ['category']
list_display = ('name', 'category_link', 'description')
search_fields = ['name', 'category__name']
admin.site.register(Animal, AnimalAdmin)
admin.site.register(AnimalImage)
And serializers.py:
from .models import Animal, Category, AnimalImage
from rest_framework import serializers
from django.contrib.auth.decorators import permission_required
class AnimalImageSerializer(serializers.ModelSerializer):
class Meta:
model = AnimalImage
fields = ['id', 'image']
class AnimalSerializer(serializers.ModelSerializer):
animal_images = AnimalImageSerializer(many=True, read_only=True)
class Meta:
model = Animal
fields = ['images', 'category', "animal_images"]
@permission_required("can_view_animal")
def get_animal(self, obj):
return obj.get_animal_display()
class SubAnimalSerializer(serializers.ModelSerializer):
category = serializers.SlugRelatedField(
slug_field='name', queryset=Category.objects.all()
)
uis = serializers.SerializerMethodField(method_name='naming_bool')
class Meta:
model = Animal
fields = ['images', "animal_images"]
read_only_fields = ['id']
def naming_bool(self, instance):
if instance.uis == True:
return "Ja"
else:
return "Nee"
So I don’t see what I am missing.
Question: how to modify the code that the propertie: “animal_images” in the api call will be filled with the uploaded images?