how to cancel a delete in django signal -


is there way cancel deletion of record using django pre_delete signal?

example:

def on_delete(sender,**kwargs):   if not <some condition>:     #cancel deletion  # else continue deletion pre_delete.connect(on_delete,sender=mymodel) 

and question there way model "that before changing file delete first original file" because right do(see code below) , i'm not sure if best way it.

def on_save(sender,**kwargs):   obj = kwargs['instance']   try:     id = obj.pk     # find file     original_file = sender.objects.get(pk=id)     # delete original file before uploading new file     original_file.file.delete()   except ....  pre_save.connect(on_save,sender=modelwithfileupload) 

(in django 1.2 automatically delete file on change or on delete in django 1.3 removed feature)

thanks in advance

i try little hack workaround:

def on_delete(sender,**kwargs):   if not <some condition>:     raise exception('do not delete')#cancel deletion  # else continue deletion pre_delete.connect(on_delete,sender=mymodel) 

and view

def on_save(sender,**kwargs):   obj = kwargs['instance']   try:     id = obj.pk     # find file     original_file = sender.objects.get(pk=id)     # delete original file before uploading new file   except ... :     # oder exceptions     try:     original_file.file.delete()   except:     pass #not deleted  pre_save.connect(on_save,sender=modelwithfileupload) 

raising exception in signal should brake delete() method execution while returning exception place invoked. create own exception subclass except type of exception (you never should use except no args).


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -