Ensure image file is deleted when NotesImage is deleted

This commit is contained in:
Oliver Walters
2026-05-25 07:07:44 +00:00
parent 4fff7ed589
commit 0df677d277
2 changed files with 9 additions and 2 deletions
@@ -115,7 +115,7 @@ class Migration(migrations.Migration):
field=models.ForeignKey( field=models.ForeignKey(
blank=True, blank=True,
null=True, null=True,
on_delete=django.db.models.deletion.SET_NULL, on_delete=django.db.models.deletion.CASCADE,
to="common.note", to="common.note",
related_name='images', related_name='images',
), ),
+8 -1
View File
@@ -3095,6 +3095,13 @@ class NotesImage(models.Model):
Simply stores the image file, for use in the 'notes' field (of any models which support markdown). Simply stores the image file, for use in the 'notes' field (of any models which support markdown).
""" """
def delete(self, *args, **kwargs):
"""Ensure that the image file is deleted from storage when the NotesImage instance is deleted."""
if self.image:
self.image.delete(save=False)
super().delete(*args, **kwargs)
image = models.ImageField( image = models.ImageField(
upload_to=rename_notes_image, verbose_name=_('Image'), help_text=_('Image file') upload_to=rename_notes_image, verbose_name=_('Image'), help_text=_('Image file')
) )
@@ -3119,7 +3126,7 @@ class NotesImage(models.Model):
) )
note = models.ForeignKey( note = models.ForeignKey(
Note, on_delete=models.SET_NULL, null=True, blank=True, related_name='images' Note, on_delete=models.CASCADE, null=True, blank=True, related_name='images'
) )