Hello, I have a model called Image:
class Image(models.Model, GenericCheckForDelete):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
url = models.CharField(validators=[URLValidator()])
alt_en = models.CharField()
description = models.CharField(blank=True, null=True)
alt_cs = models.CharField()
class Meta:
managed = False
db_table = 'planner"."image'
def __str__(self) -> str:
return self.alt_en
I want to implement a way to upload an image without adding Image/FileField to the model. I want to do it using ModelForm like this:
class ImageForm(forms.ModelForm):
image_file = forms.FileField(label="Image file", required=True)
class Meta:
model = Image
exclude = []
@admin.register(Image)
class ImageAdmin(UniversalModelAdmin):
def image_preview(self, obj):
"""
Add image preview based on url field value
"""
try:
return format_html(f'<img src="{obj.url}" style="max-width:100px; max-height:100px"/>')
except:
return None
form = ImageForm
list_display = [field.name for field in Image._meta.fields] + ["image_preview"]
fields = [field.name for field in Image._meta.fields if field.name != "id"] + ["image_preview", "image_file"]
readonly_fields = ["image_preview"]
But in ModelForm, there is no upload_to attribute. How do I upload images and save them to Amazon S3? Is there such an option in admin interface? Thanks for your help