Задать вопрос

Тел: +7 965 3737 888

473

Просмотров

4

Ответов

Use the primary key in FileField and ImageField filenames

<p>Sometimes it is desirable to use values like the primary key when naming FileField and ImageField files, but such values are only available after saving the model instance.  This abstract class implements a two-phase save in order to make this case easy.  See the example in the docstring.
</p>
<p>Another solution would be to write a save() that requires upload_to to be a callable that checks for instance.pk, then calls it again after saving.  However, this would require more work from the developer for simple cases.
</p>

Вопрос полезен? Да0/Нет0
file_4171.py(2.9Кб)
None

Ответы (4):

Ответgiles:16.05.2013
Ответ полезен? Да0/Нет0

@grahamu @abusquets

To fix that error, you should close the file field before you delete it: field.close()

Ответnemesis:16.11.2010
Ответ полезен? Да0/Нет0

Nice one, thanks!

Ответabusquets:21.03.2009
Ответ полезен? Да0/Нет0

I have the same problem as grahamu

Ответgrahamu:16.12.2008
Ответ полезен? Да0/Нет0

FWIW, this class did not work for me when running Django in Windows XP. Specifically, the field.storage.delete(file_name) call results in Windows "Error 32: The process cannot access the file because it is being used by another process".