3

I am working on a django/python project. This project uses a model that contains a FileField. This model is fed by a web form. Everything works great: Django automatically renames the file if an existing file with the same name already exists.

I have a second Model that also contains a FileField. But this file is not uploaded via a web form. This file content is generated on the file by a python program. What I want to do is provide a file name when inserting it. And I want Django/Python to rename automatically as it does no the web form. How should I do ?

Thanks.

John Källén
  • 7,551
  • 31
  • 64
Bob5421
  • 7,757
  • 14
  • 81
  • 175

2 Answers2

4

I think the simplest way to do this is explained in this answer:

https://stackoverflow.com/a/10501355/3848720

You could either just call the filename a universally unique identifier (UUID) as in the answer above. Or you could append the UUID onto the existing name as follows:

import uuid

filename = '%s%s' % (existing_filename, uuid.uuid4())
Community
  • 1
  • 1
tdsymonds
  • 1,679
  • 1
  • 16
  • 26
  • I do not think that it works this way when uploading because the suffix does not look to an uuid. In fact i want to know if this automatic renaming is done per FileField or by something else in django – Bob5421 Nov 10 '16 at 21:33
1

I think using UUID will make your filename so long. This may also help you :-

  import os, random, string
  length = 13
  chars = string.ascii_letters + string.digits + '!@#$%^&*()'
  random.seed = (os.urandom(1024))
  a = ''.join(random.choice(chars) for i in range(length))

Note :- Adjust the length parameter as per your wish.

filename = '%s%s' % (Yourfilename,str(a))

Change models.py as:

fileName = CharField(max_length=10, blank=True,unique=True)

Change views.py as:

try:
    filename  = filename
    storeFileName = models.objects.create(fileName=filename) 
except Exception,e:
   import os, random, string
   length = 13
   chars = string.ascii_letters + string.digits + '!@#$%^&*()'
   random.seed = (os.urandom(1024))
   a = ''.join(random.choice(chars) for i in range(length))

   filename = '%s%s' % (Yourfilename,str(a))

   storeFileName = models.objects.create(fileName=filename)

You can't set unique=True for fileField this might be bugs in Django it will work as follows:

Apparently, what happens is this: If the file already exists, the new file will be named <>., where <> is the number of underscores needed to make the name unique. So, for example, when a file named foo.png is uploaded multiple times, the second file will be named foo_.png, the third will be foo__.png and so on. This should probably be documented, though. I'm not sure whether this renaming happens in the admin code or in the model code.

So finnaly, you may have only one option to save filename seperately and check in that before uploading file.

Piyush S. Wanare
  • 4,703
  • 6
  • 37
  • 54
  • Thanks but when file do not exist, i want to keep the original name. What should i do if 2 process access at the same time with the same filename... Is it possible to make atomic the test of existing file ? – Bob5421 Nov 11 '16 at 07:58
  • then you should add in model fileField as unique filed. And in script do try for storing file if database if got duplicate you can write exception and change filename and then store. – Piyush S. Wanare Nov 11 '16 at 08:04
  • Thanks but this is not a FileField ? – Bob5421 Nov 11 '16 at 08:15