In my project I have an app with a school model and a schoolsuggestion model.
The school model has fields for various images, like logo, seal, etc:
seal_image = models.ImageField(upload_to="sealimgs", null=True, blank=True)
logo_image = models.ImageField(upload_to="logos", null=True, blank=True)
The schoolsuggestion model has a field for what the corresponding school field is, and an image field.
field_name = models.CharField(max_length=255, default="")
image_field = models.ImageField(upload_to='img', null=True, blank=True)
When a school suggestion is approved, I overwrote save so that it will get the associated school’s field via getattr(field_name) and set it with setattr(field_name, image_field) (I’m simplifying the syntax, but you get the idea).
Problem is, the file is still being saved in img, presumably because I’m using setattr and not the image field specific save, which would have uploaded it to the correct directory.
So how do I do this? It makes sense that I could maybe get the upload_to value of the attribute somehow, resave the image with that filepath somehow, and then use setattr on the resaved image, but I can’t figure out how to do the first two.
I modified the upload to to this function and included it in the file above the class def: