Auto delete image files when object has been deleted

This commit is contained in:
Antoine Bartuccio 2025-04-10 10:35:17 +02:00
parent 3e61560875
commit 0f961c71e0
2 changed files with 16 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# Generated by Django 4.2.20 on 2025-04-06 09:55
# Generated by Django 4.2.20 on 2025-04-10 08:14
import django.db.models.deletion
from django.conf import settings
@ -25,7 +25,14 @@ class Migration(migrations.Migration):
),
("uuid", models.UUIDField(db_index=True, unique=True)),
("name", models.CharField(max_length=100)),
("image", models.ImageField(upload_to="upload/%Y/%m/%d")),
(
"image",
models.ImageField(
height_field="height",
upload_to="upload/%Y/%m/%d",
width_field="width",
),
),
(
"created_at",
models.DateTimeField(auto_now_add=True, verbose_name="created at"),

View File

@ -1114,7 +1114,9 @@ class QuickUploadImage(models.Model):
uuid = models.UUIDField(unique=True, db_index=True)
name = models.CharField(max_length=IMAGE_NAME_SIZE, blank=False)
image = models.ImageField(upload_to="upload/%Y/%m/%d")
image = models.ImageField(
upload_to="upload/%Y/%m/%d", width_field="width", height_field="height"
)
uploader = models.ForeignKey(
"User",
related_name="quick_uploads",
@ -1155,11 +1157,13 @@ class QuickUploadImage(models.Model):
name=name,
image=file,
uploader=uploader,
width=width,
height=height,
size=file.size,
)
def delete(self, *args, **kwargs):
self.image.delete(save=False)
return super().delete(*args, **kwargs)
class LockError(Exception):
"""There was a lock error on the object."""