The reason it's generating the same one every time is that the code as written executes when Django starts and that name is kept until you restart your Django process.

You can use a lambda to fix that in general

Example:

    wrong:
        #all records get the datetime the Django process was launched
        created_date = models.DateTimeField(default = datetime.now())

    right (ignoring the auto_now_add shortcut):
        #datetime.now() will be executed each time an instance is created
#you can also forgo the lambda and the parentheses, and just use datetime.now,
        #because it's callable
created_date = models.DateTimeField(default = lambda: datetime.now())

According to the Django docs you can do this with upload_to as well:
https://docs.djangoproject.com/en/1.3/ref/models/fields/#filefield
"This may also be a callable, such as a function, which will be called to obtain the upload path, including the filename"

Rather than using random, I recommend using the tempfile module in Python's standard library:
http://docs.python.org/library/tempfile

You can use mkdtemp to create a unique directory and mkstemp to create a filename if you want that to be unique as well. That will ensure your random path doesn't accidentally overwrite any other. Don't be fooled by the 'temp' part; you're free to provide a starting path for all "temp" directories (including in your 'uploads' folder), and they're not deleted automatically, so you can keep them permanently or prune them as needed. If you don't care you can use gettempdir and it'll put the file in your OS's default temp folder (probably /tmp) and it'll be removed on reboot (or by you manually).





--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to