def generate_filename_and_delete_previous(ffile, name, before_delete=None):
 new_filename = ffile.field.generate_filename(ffile.instance, name)
 try:
  orig_instance = ffile.instance.__class__.objects.get(id=ffile.instance.id)
  orig_field_file = getattr(orig_instance, ffile.field.name)
  orig_filename = orig_field_file.name
  if (orig_filename and (new_filename != orig_filename)):
   if before_delete:
    before_delete(orig_field_file)
   orig_field_file.delete()
 except ffile.instance.__class__.DoesNotExist:
  pass
 return new_filename

def generate_filename_and_delete_previous ( ffile , name , before_delete = None ) : new_filename = ffile . field . generate_filename ( ffile . instance , name ) try : orig_instance = ffile . instance . __class__ . objects . get ( id = ffile . instance . id ) orig_field_file = getattr ( orig_instance , ffile . field . name ) orig_filename = orig_field_file . name if ( orig_filename and ( new_filename != orig_filename ) ) : if before_delete : before_delete ( orig_field_file ) orig_field_file . delete ( ) except ffile . instance . __class__ . DoesNotExist : pass return new_filename
generate a new filename for a file upload field; delete the previously uploaded file .