Advancing-the-Blog
Advancing-the-Blog copied to clipboard
upload_location not working for the first post
def upload_location(instance, filename):
#filebase, extension = filename.split(".")
#return "%s/%s.%s" %(instance.id, instance.id, extension)
PostModel = instance.__class__
new_id = PostModel.objects.order_by("id").last().id + 1
return "%s/%s" %(new_id, filename)
This does not work if there is no post in the database. Add some exception handling and set new_id default to 1 would work.
Also, use post_delete signal to delete image file when deleting a post instance would be good.