Moving a file manually

5 views
Skip to first unread message

Tor Nordam

unread,
Jan 26, 2012, 3:44:42 AM1/26/12
to Django users
Hello,

I have a model with a FileField, that I use to store some user
uploaded files. After a while, I changed my mind about where to store
the files, so I was just wondering if there is a way to manually move
a file, and then update the location of the file as stored in the
database. I'm using a plain models.FileField, with no custom Storage
or anything advanced.

Cheers,
Tor

Jonathan Paugh

unread,
Jan 26, 2012, 5:15:49 AM1/26/12
to django...@googlegroups.com
You could write a short script to do the rename, and run it under
"./manage.py shell". Of special interest are the os.path.basename() and
os.path.join() functions. I haven't worked with FileFields, though, so
be wary of my advice.

something like this:
import os.path
from app.models import Model
prefix = #new folder location
ms = Model.objects.all()
for m in ms:
oldname = m.fileField.?? #get old path name
basename = os.path.basename(oldname) # Get the filename by itself
newpath = os.path.join(prefix, basename)
#store it back on the object
m.save()


Reply all
Reply to author
Forward
0 new messages