I have a Model containing a FileField. I'd like this FileField to have a unique path.
At first, at though about using the ID of the entry, but Django move the file to it's upload_to path before saving the entry, so ID is empty.
Moreover, I can't use something like the title or any other elements of the model (except the creation date) since they can be changed by the user. And I prefer not to copy/delete a file every time a user change the title of it's entry (if I use the title as a part of my path).
Here start my research, I found these :
Generate a unique key and compare it to the database. While the key exist, we generate a new one (Django, unique field generation) : The problem is the potentials hits the while could do to the database before having a unique key
Getting the timestamp from the creation date. The problem here, is if two people add a file at the exact same time, it will generate conflicts
I'd like to have this unique ID a small as possible, max length of 7 would be great. The perfect solution would have been to have the ID of the entry. Do you know a workaround to do so (calling save() before moving files to their upload_to folder?) or if not, which implementation would be the best, based on one of my solutions or a one you think is better?