WebFaction
Community site: login faq

I have a django app with a model featuring a FileField for uploading PDF files from within the django admin. The files are uploaded to a Webfaction static app with .htaccess. I can create an instance object of the model with a file upload from within the django admin and it works fine. The file is saved to the specified directory in my static app and all database data is saved without a problem. I run into a problem, though, when I delete the object from within the django admin. The object is deleted from the database but the object's uploaded file is not deleted from the static directory.

In my development environment, a BitNami DjangoStack with an Apache server, the uploaded file is deleted when I delete its associated object. It's my understanding that this is how a FileField is supposed to work.

Has anyone encountered the same issue? I'm a rookie developer and can't for the life of me figure out what is preventing the file from being deleted. Anyone have any ideas about what I might have done wrong? Thanks.

asked 22 Jul '11, 14:49

toms
111
accept rate: 0%


Yes, that is the way Django's filefield works. However, you can use the pre_delete signal, https://docs.djangoproject.com/en/1.3/ref/signals/#pre-delete, to create a function that deletes the files when the record has been deleted.

def delete_before(instance, **kwargs):
    (Your function code here)
signals.pre_delete.connect(delete_before, sender=(Model to attach to))
permanent link

answered 22 Jul '11, 17:19

bmeyer71 ♦♦
1.5k3613
accept rate: 33%

bmeyer71, Thanks for the reply. I may have not explained my issue clearly. In my development environment (Bitnami DangoStack: django, mod_python, apache), when I delete my model's instance object from within the django admin the object's uploaded file is also deleted from my upload directory--without my having to provide any instructions via a pre-delete method. This is the default FileField behavior, right? However, with the same code deployed to my Webfaction server, when I perform the same action the uploaded file is not deleted from my static upload directory (the object itself is deleted from the db, though). Why would I need to code a pre-delete method in the Webfaction environment when it wasn't required in my development environment? I'm running Django 1.2 in my dev environment and 1.3 when deployed. Did FileField behavior change between these versions? Thanks.

(23 Jul '11, 15:12) toms

What is the exact version of Django you are using?

As of 1.2.5 filefield will no longer delete files associated to a filefield. See this release note for further information. https://docs.djangoproject.com/en/1.3/releases/1.2.5/#filefield-no-longer-deletes-files.

(23 Jul '11, 15:19) bmeyer71 ♦♦

Just read the release info. That was it. I'm running version 1.2.3 in my dev environment. Guess I'll have to go back and code the pre-delete methods or figure out how to run a cron job to get rid of any orphaned files. Thanks for the help, bmeyer71.

(23 Jul '11, 15:29) toms

You are welcome.

I am actually using the pre-delete method on a project I am working on, and it works great. No need to code something else to delete the files. They are deleted when the record is deleted in the admin or in the client facing interface.

(23 Jul '11, 15:33) bmeyer71 ♦♦
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×909
×15
×5

question asked: 22 Jul '11, 14:49

question was seen: 5,474 times

last updated: 15 Mar '12, 14:57

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM