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!
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
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.
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?
Who do I talk to about possibly contributing to the docs?