Showing posts with label reminder. Show all posts
Showing posts with label reminder. Show all posts
Thursday, June 29, 2017
Django Reminder Delete File when deleting object
Django Reminder Delete File when deleting object
For Django FileField objects and object of its subclasses, when using the objects delete() method, it will only delete the object record from the database, the file will remain in the appointed location in your servers filesystem.
After a few research, it is a deliberate design in Django to prevent unwanted mess in database when rolling back an record (not sure how this is doable though, leave comments if you know how :D).
So, we have to explicitly delete the file from the filesystem ourselves using
FileFieldObject.file_field.storage.delete(filepath)
A simple implementation to delete the file when delete the file object from db would be like this:
f = FileFieldObject #replace with your models .objects.get() here
storage = f.file_field.storage
#field_field is the property in your model which is an object of class model.FileField
path = f.field_field.path
storage.delete(path) #delete file from disk
f.delete() #delete record from db
Saturday, June 24, 2017
Diablo 3 Season 2 and Dream Of Mirror Online Both Start Today! Reminder
Diablo 3 Season 2 and Dream Of Mirror Online Both Start Today! Reminder

Just a quick post as a reminder: Diablo 3 Season 2 and Dream Of Mirror Online both started today!
Get in there and start levelling - no matter which game you are playing this weekend, have fun - and See You In The Games!
Wednesday, April 19, 2017
Django Reminder Handling Custom Exceptions
Django Reminder Handling Custom Exceptions
In Django, if we try to get an object that does not exist, the DoesNotExist exception will be raised.
However, the following code does not work out of the box, albeit being the obvious solution to handle such exception:
try:
img = Image.objects.get(pk = 42)
except DoesNotExist:
print "Image Does Not Exist"
This is because the DoesNotExist exception is actually an attribute of an model object.
Therefore, we would need to use this exception express to handle it (so we dont have to import the error manually)
except Image.DoesNotExist:
print "Image Does Not Exist"
To handle more than one possible exception, use the standard Python way to add more exception cases:
except (ValueError, Image.DoesNotExist):
#do something.
Labels:
custom,
django,
exceptions,
handling,
reminder
Subscribe to:
Posts (Atom)