How to handle deleting a file associated with a database record? -


my web application allows user upload image files, , file url included field in database record. instance, have post table schema like:

title text topimage tag posttime 

here topimage url image uploaded when user writing post. question is: how gracefully delete image file when db record deleted? instance, when above post deleted, image attached no longer needed. want asynchronously, because file on cdn, need call api remove it.

my first thought use filerecord table record each uploaded file, add foreign key in post table. , when post deleted, mark file record 'to deleted'. can use background job periodically clear 'shadow files'.

but solution has problem: need remember mark file record every scenario of post deletion. if have postcollection table, includes posts. when collection deleted, post records automatically deleted (cascade), have manually set every image file 'to deleted'. because app has many levels of data models, solution not elegant.

what do?

edit

i end using django's signal: when record deleted, signal triggers handler deal file deletion, works cascaded delete. there case: when file field updated new file, need delete old one. django built-in signals don't here. can implement own signal, now, manually mark old file deletion.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -