1👍
In order to upload multiple images for each product you have to modify your model as you have already designed. Let’s see:
Your Product model should look like this-
class Product(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
name = models.CharField(max_length=200, null=True, blank=True)
brand = models.CharField(max_length=200, null=True, blank=True)
category = models.CharField(max_length=200, null=True, blank=True)
description = models.TextField(null=True, blank=True)
rating = models.DecimalField(
max_digits=7, decimal_places=2, null=True, blank=True)
mum_of_reviews = models.IntegerField(null=True, blank=True,
default=0)
price = models.DecimalField(
max_digits=7, decimal_places=2, null=True, blank=True)
stock_counter = models.IntegerField(null=True, blank=True,
default=0)
created_at = models.DateTimeField(auto_now_add=True)
_id = models.AutoField(primary_key=True, editable=False)
Let’s create a new model to add multiple images for each product-
class ProductImage(models.Model):
product = models.ForeignKey(Product, on_delete=models.SET_NULL,
null=True, related_name='product_images')
image = models.ImageField(null=True, blank=True,
default='/placeholder.png')
Let’s write the serializer for them
class ProductImageSerializer(serializers.ModelSerializer):
class Meta:
model = ProductImage
fields = '__all__'
class ProductRetriveSerializer(serializers.ModelSerializer):
reviews = serializers.SerializerMethodField(read_only=True)
images = serializers.SerializerMethodField(read_only=True)
class Meta:
model = Product
fields = '__all__'
def get_reviews(self, obj):
reviews = obj.review_set.all()
serializer = ReviewSerializer(reviews, many=True)
return serializer.data
def get_images(self, obj):
images = obj.product_images.all()
return ProductImageSerializer(images, many=True)
class ProductCreationSerializer(serializers.ModelSerializer):
images = ProductImageSerializer(many=True, required=False)
class Meta:
model = Product
fields = '__all__'
Let’s write the API for creating a product with multiple images
class ProductCreateAPIView(APIView):
parser_classes = (FormParser, MultiPartParser)
serializer = ProductCreationSerializer
@transaction.atomic
def post(self, request, format=None):
images = request.data.get('images', [])
request.data.pop('images')
serialized_data = self.serializer(data=request.data)
product_obj = None
if serialized_data.is_valid():
product_obj=Product.objects.create(**serialized_data.validated_data)
if product_obj and len(images) > 0:
for image_data in images:
image_data['product_id'] = str(product_obj._id)
product_image_serialized_data = ProductImageSerializer(data=image_data)
if product_image_serialized_data.is_valid(raise_exception=True):
product_image_obj = ProductImage.objects.create(**product_image_serialized_data.validated_data)
# Write your necessary code if needed
return Response(data=serializer.data, status=status.HTTP_201_CREATED)
I have at least tried to make you understand the way you will upload multiple images.Best of luck 🙂
Source:stackexchange.com