I have a Django app with an image field (a custom ThumbnailImageField type) that auto-generates the file path for an image based on the title, type, and country of the item the image is attached to (upload_ to = get_ image_path). Here's how:
def get_image_path(instance, filename):
dir = 'images'
subdir = instance.get_type_display()
sub_subdir = 'other'
if instance.country:
sub_subdir = instance.country.name
name = instance.name
extension = filename.split('.')[-1]
return "%s/%s/%s/%s.%s" % (dir, subdir, sub_subdir, name, extension)
It works great, except in one situation: When I rename an item, change the country it's from, or change the category it's in, the image becomes a dead link because it generates a new image path without moving the orignal file.
So, the magic question:
Is there some save function in Django that I can hook into and override that will let me have the original object and the proposed values and compare them so I know where the image path was and where it will need to go (and then use this info to move/rename in code)?