I am using an ImageForm
to upload images to my database and I store them in base64 (user’s icons, only some pixels). But the update destroyed the functionality:
class CustomImageFile(AdminFileWidget):
def render(self, name, value, attrs=None, renderer=None):
output = [super().render(name, value)]
try:
output.append(self.form_instance.instance.show_picture())
except:
pass
return "".join([x for x in output])
class PersonAdminForm(forms.ModelForm):
image_file = forms.ImageField(widget=CustomImageFile, required=False, label="picture")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["image_file"].widget.form_instance = self
class Meta:
model = Person
fields = ["name", ..., "image_file"]
@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
list_display = ["get_name", "get_picture"]
@admin.display(description="...")
def get_picture(self, obj):
try:
return obj.show_picture
except:
return "... "
class Person(models.Model):
name = models.CharField("name", max_length=128, blank=True, null=True)
picture = models.TextField("asb64", blank=True)
@property
def show_picture(self):
try:
return format_html(f"""<img src="{self.picture}">""")
except:
return ""
I tested this with Django 4.2.6 & 4.2.13 which yields:
After an upgrade to 5.0.6:
Where there changes to ImageField? I have not yet found something in the patchnotes I could use, but would be glad for a solution.