0

i have a list of base64 images that i send from a react panel. images list look like below:

[
"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkG",
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeAAAADkCAY"
]

Note: i remove most of the charachters because each index charachters is more than 2000 and it's just a example of what i have.

so i want to save them in django model. my field for saving this list of images is this:

ArrayField(base_field=models.ImageField(upload_to='hotel_images/'))

so how should i convert list of base64 images to real images and save them to this ArrayField? or do you have a better way for saving this base64 images list?

Note: i convert images to base64 in react panel by code below:

const FR = new FileReader();

FR.addEventListener("load", function () {
   // FR.result is image converted to base64
   image = FR.result;
})

FR.readAsDataURL(img);

and i do this because i have a json that contains other fields like name and description so when i want to send this json with request body i should use JSON.stringify() for that and when i use JSON.stringify(), images field is turn to empty array and i have to convert images to base64. so if you have a solution for this problem, that could help too!

Final Note: my database is postgreSQL.

erfanmashari
  • 311
  • 3
  • 7

1 Answers1

0

you can use ListField and Base64ImageField as child in your serializer class


from rest_framework import serializers
from drf_extra_fields.fields import Base64ImageField

images = serializers.ListField(
      child=Base64ImageField(required=False), required=False
  )
K.D
  • 152
  • 6