How do I rename a FileField() that's attached to a model?

2,505 views
Skip to first unread message

Jumpfroggy

unread,
Mar 13, 2010, 8:55:08 PM3/13/10
to Django users
I've gone through the documentation a ton, been trying different
techniques, and I know I'm just missing something obvious.

I have a model like this:

class MyModel(Model):
file1 = FileField(...)

So I can do this:

model = MyModel.objects.get(id=1)
print mode.file1.name

But I want to rename the file on the filesystem. I want something
like this:

model.file1.rename(new_filename)

But that doesn't exist. The closest I can find is somethig like this:

contents = model.file1.read()
model.file1.delete()
model.file1.save(new_filename, contents)

But this gives an exception since the FileField save() method requires
a "contents" parameter that has a ".chunks()" method. Right now it's
just a string.

I can't use a python tempfile.TemporaryFile() object, since that then
gives the exception "Unable to determine the file's size".

All of this seems like I'm jumping through hoops that don't make
sense. How do I just rename the file? Is there an easier way?

Thanks so much!

Kenneth Gonsalves

unread,
Mar 13, 2010, 9:08:10 PM3/13/10
to django...@googlegroups.com
On Sunday 14 Mar 2010 7:25:08 am Jumpfroggy wrote:
> But I want to rename the file on the filesystem. I want something
> like this:
>
> model.file1.rename(new_filename)
>

you could do it manually. first use os.rename to rename the file on the disk,
and then update your model instance with the new name and save it.
--
regards
Kenneth Gonsalves
Senior Associate
NRC-FOSS
http://certificate.nrcfoss.au-kbc.org.in

Jumpfroggy

unread,
Mar 14, 2010, 3:10:30 AM3/14/10
to Django users
> you could do it manually. first use os.rename to rename the file on the disk,
> and then update your model instance with the new name and save it.

I think this is what I'm missing. What's the code for this? Can I
just do:

os.rename(model.file1.name, new_filename)
model.file1.name = new_filename
model.save()

Is that enough? I'll go try that now. The problem is that the
documentation leaves it unclear whether simply changing the name and
saving the model is kosher. I guess I'd expect a more complicated
method, but if that works then great.

Jumpfroggy

unread,
Mar 14, 2010, 3:19:44 AM3/14/10
to Django users
Yes, it was that easy. I don't know why, but I've been banging my
head against the wall on this for a while and never thought of this
solution.

One thing that would really help a ton is more/better examples about
file-handling in django. I've spent more hours on this single feature
reading & rereading the docs trying to figure out how things work. A
few good examples would have saved me tons of time.

Who do I talk to about possibly contributing to the docs?

Karen Tracey

unread,
Mar 14, 2010, 10:15:24 AM3/14/10
to django...@googlegroups.com
On Sun, Mar 14, 2010 at 2:19 AM, Jumpfroggy <rocket...@gmail.com> wrote:
Who do I talk to about possibly contributing to the docs?

Contributing to the docs is handled just like contributing to the code. They are all in SVN, under a docs directory in the trunk.  See http://docs.djangoproject.com/en/dev/internals/contributing/ for general info on contributing.

Karen
Reply all
Reply to author
Forward
0 new messages