0
def image_saving_product(instance, filename):
    return '/'.join( ['product', str(instance.id), filename] )

models.py

class Image(models.Model):
   image_file = models.ImageField(blank=True,upload_to=image_saving_product,null=False)
   product = models.ForeignKey(ProductInfo,on_delete=models.PROTECT,default=None, blank=False)
   def __str__(self):
       return f'image id is {self.id} & this image is for {self.product.id},s product'
   def __unicode__(self):
       return f'image id is {self.id} & this image is for {self.product.id},s product'

serializers.py

class ImageSerializer(serializers.ModelSerializer):

class Meta:
    model = Image
    fields = '__all__'

views.py

class ProductImageViewSet(viewsets.ModelViewSet):
   authentication_classes = [TokenAuthentication]
   permission_classes = [IsAdminUser]
   queryset = Image.objects.all()
   serializer_class = ImageSerializer

enter image description here

this is my code, I can send post request when I wanna post Image but it's value is null.. I'll be thankfull if you answer my Question.

1 Answers1

1

You send Image NOT image_file so change it in postman and will work fine.

Mohamed Hamza
  • 945
  • 1
  • 5
  • 15
  • What does this mean? How do you send this? We go into Body -> form-data -> change key from text to file -> select files and upload -> Send. This is it right? – Vishal Rana Nov 22 '22 at 11:25