For example if i want to delete all uploaded ima开发者_C百科ges from filesystem and database row of instance of have model like this:
class PictureSet(models.Model):
image1 = models.ImageFIeld(upload_to="images/")
image2 = models.ImageFIeld(upload_to="images/")
image3 = models.ImageFIeld(upload_to="images/")
Have i any shorter possibility to do this, than [example]:
picture_set1 = PictureSet.objects.get(id=1)
picture_set1.image1.delete()
picture_set1.image2.delete()
picture_set1.image3.delete()
picture_set1.delete()
???
I'd suggest overriding model's delete method:
def delete(self, *args, **kwargs):
for field in self._meta.fields:
if type(field) == models.ImageField:
image = self.__getattribute__(field.name)
image.delete()
super(self.__class__, self).delete(*args, **kwargs)
And then just:
PictureSet.objects.get(id=1).delete()
picture_set = PictureSet.objects.get(id=1)
picture_set1.delete()
deletes the whole pictureset class no need to delete everything...
if the pictures themselves stay there just implement some kind of cleaning functions which checks againstthe upload folder and the image links stored in database
hope this helps
精彩评论