I want the filenames to be random and therefore I use upload_to
function which returns a random filename like so:
from uuid import uuid4
import
You could go the route of populating the filename during the save process. Obviously you'll have to store the original file name in memory when your get_random_filename runs.
# inside the model
class FooModel(models.Model):
file = models.FileField(upload_to=get_random_filename)
filename = models.CharField(max_length=128)
def save(self, force_insert=False, force_update=False):
super(FooModel, self).save(force_insert, force_update)
#Do your code here...
Just re-order your commands. https://docs.djangoproject.com/en/dev/topics/db/models/
def save(self, *args, **kwargs):
do_something()
super(Blog, self).save(*args, **kwargs) # Call the "real" save() method.
do_something_else()
The posted code normally works, perhaps the actual code is
class FooModel(models.Model):
filename = models.CharField(max_length=128)
file = models.FileField(upload_to=get_random_filename)
Note the switching of the ordering of the fields above.
This won't work because: the upload_to()
is invoked by the pre_save()
, here in the code, when the actual value of the FileField
is required. You could find that the assignment to the attribute filename
in the upload()
is after the generating of the first param filename
in the inserting sql. Thus, the assignment does not take effect in the generated SQL and only affects the instance itself.
If that's not the issue, please post the code you typed in shell.