ARTICLE AD BOX
I have the following model:
from django_resized import ResizedImageField class PostImage(models.Model): image = ResizedImageField( size=[1200, 1200], quality=85, upload_to="post_images/", blank=True, null=True, force_format="WEBP", keep_meta=False, width_field="width", height_field="height", ) width = models.IntegerField(null=True, blank=True) height = models.IntegerField(null=True, blank=True)Now the problem I am facing is:
If the image file does not exist, it throws an exception. I was expecting for it to just send the image path, if it does not exist, the frontend will show the alt value or whatever. The reason it throws an error, according to chatgpt, is because it's evaluating the width and height during request time, so it needs to access the file on every request I guess?Claude is suggesting to remove the width_field and height_field altogether, and in the create() method of the serializer, do the following:
from PIL import Image as PilImage for image_data in uploaded_images: img = PilImage.open(image_data) width, height = img.size image_data.seek(0) PostImage.objects.create(post=post, image=image_data, width=width, height=height)Is this okay? I couldn't find a solution online that is used by actual industries.
