Hi folks,
I have various models with File and Image fields. I want to keep the
filesystem storage clean, removing any orphaned file upon a new one
being uploaded. To date I've been overriding save() in each model,
which isn't terribly robust:
class MyModel
def save(self, **kwargs):
# remove old file if new one is uploaded
old_obj = MyModel.objects.get(pk=self.pk) if self.pk else None
if old_obj and self.image != old_obj.image:
old_obj.image.storage.delete(old_obj.image.name)
super(MyModel, self).save(**kwargs)
For obvious reasons I'd like to refactor this into a custom FileField/
ImageField. The logic that I would like is even simpler and more
reliable than the above:
1. If a new file was provided, delete the old one.
2. Save.
The trouble is I can't figure out how, at the Model or Field level, to
figure out if a new physical file was *really actually provided*, in
order to decide whether I should delete the underlying old one.
Thanks for any help on this one.
Regards
Scott
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.