mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-06 05:35:19 +02:00
fix(models): ensure associated Attachment files are deleted and their filesystem cleaned up on Adventure deletion
This commit is contained in:
parent
07346bf03d
commit
bed72e92da
1 changed files with 8 additions and 0 deletions
|
@ -660,6 +660,9 @@ class Adventure(models.Model):
|
||||||
# Delete all associated AdventureImages first to trigger their filesystem cleanup
|
# Delete all associated AdventureImages first to trigger their filesystem cleanup
|
||||||
for image in self.images.all():
|
for image in self.images.all():
|
||||||
image.delete()
|
image.delete()
|
||||||
|
# Delete all associated Attachment files first to trigger their filesystem cleanup
|
||||||
|
for attachment in self.attachments.all():
|
||||||
|
attachment.delete()
|
||||||
super().delete(*args, **kwargs)
|
super().delete(*args, **kwargs)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
@ -861,6 +864,11 @@ class Attachment(models.Model):
|
||||||
adventure = models.ForeignKey(Adventure, related_name='attachments', on_delete=models.CASCADE)
|
adventure = models.ForeignKey(Adventure, related_name='attachments', on_delete=models.CASCADE)
|
||||||
name = models.CharField(max_length=200, null=True, blank=True)
|
name = models.CharField(max_length=200, null=True, blank=True)
|
||||||
|
|
||||||
|
def delete(self, *args, **kwargs):
|
||||||
|
if self.file and os.path.isfile(self.file.path):
|
||||||
|
os.remove(self.file.path)
|
||||||
|
super().delete(*args, **kwargs)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.file.url
|
return self.file.url
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue