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.
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())
- [Django]-Deploying Django, Gunicorn, Nginx, Virtualenv on Digital ocean gives me 502 Bad Gateway & Gunicorn can't read Secret Key
- [Django]-Wagtail SnippetChooserBlock in Streamfield
- [Django]-Excluding password when extending UserCreationForm
- [Django]-Django makemessages command generates error: "xgettext: Non-ASCII string"
- [Django]-Django: How to keep track of a linear (yet flexible) project management workflow?