3

I am saving some uploaded files with a Django FileField set to use DefaultStorage backend. At some point after the file has been uploaded I'd like to move them to a different storage backend i.e. change the FileField's storage attribute (obv. after saving the contents of the source file to the new storage location). Simply changing the FileField instances storage doesn't seem to work.

Is this possible without the use of a second FileField model attr which has been told to use a different storage backend? Ideally I'd like to not have to double up on the fields and put switches in all the templates that reference the files.

Thanks!

markhellewell
  • 24,390
  • 1
  • 21
  • 21
  • You wouldn't need to "put switches in all the templates that reference the files" if you doubled up the fields - put the logic for determining which field to use in a method on your model, and then call that method in your template. – Dominic Rodger Aug 12 '10 at 07:08
  • There might be no way around having a field for each location, but it feels a little clunky when all I really want to do is tell an instance of my models FileField that its file lives somewhere else now. – markhellewell Aug 12 '10 at 07:14
  • OK, so I've implemented it as a second FileField.. not so bad :) – markhellewell Aug 12 '10 at 08:56

1 Answers1

0

It seems that the storage associated with a FileField is not written to the database, so setting it on a particular field instance wouldn't persist. Instead it is read from the FileField instance associated with the model (so if you have file = models.FileField(..., storage=some_storage) it is reset to some_storage every time the models are setup by Django).

Zach Snow
  • 1,014
  • 8
  • 16