UUID for unique directory name

92 views
Skip to first unread message

Amelie Bacle

unread,
Aug 25, 2014, 12:02:46 PM8/25/14
to django...@googlegroups.com
Hello everybody,

I'm developing an app for the first time with Django and I have a problem with upload path and UUID.
I have a form with 3 fields for uploading text files. So I want to save those 3 files in the same directory which have a unique name. First I did this a big random number but if 2 requests are submitted almost at the same time I could generate the same number for those 2 which is not good. So I tried with the UUID method. To do so, I put a extra field in my model for the UUID which I generate in my controller at submission. Now I have a problem, how can I get the value of the UUID field to use it in the upload_to value of the FileField ? 

Here is my Model : 

class Analysis(models.Model):
uuidDir = models.CharField(max_length = 36)
dirFile = 'documents/%Y/%m/%d/' + str(uuidDir) #??
structureFile = models.FileField(upload_to = dirFile)
aliphFile = models.FileField(upload_to = dirFile)
vdwFile = models.FileField(upload_to = dirFile)
distGlyc = models.FloatField()
defaultType = models.CharField(max_length = 8)
outputName = models.CharField(max_length = 10)

Is there a method that I can call on my uuidDir field to get his value or am I thinking completely wrong on this ? 
Thanks in advance for any help !

Amélie.

Collin Anderson

unread,
Aug 25, 2014, 2:00:23 PM8/25/14
to django...@googlegroups.com
Roughly something like this:

def dir_file(analysis, file_name):  # should live outside your model
   
return '%s/%s' % (analysis.uuidDir, file_name)

then models.FileField(upload_to=dir_file)
    

Amelie Bacle

unread,
Aug 25, 2014, 2:06:42 PM8/25/14
to django...@googlegroups.com
Thanks for this, I was sure there were something to get access to the value of a field ! 
Reply all
Reply to author
Forward
0 new messages