The simplest way is to let Django do it for you by doing nothing special
at all (this is the default behaviour). When Django is trying to save a
file, it first checks that no file exists with that name. If one does,
it modifies the name slightly and repeats until a unique name is arrived
at.
Try it out for yourself: create a simple model that has a name and a
file upload field. Add two instances via the admin and uploading the sme
file. You'll notice that two copies are stored on disk, with two
different names.
Regards,
Malcolm
That's kind of an odd requirement. The filename says nothing about the
resource -- if two names are the same, it doesn't mean the contents are.
Also, the name you store it under on the filesystem isn't (necessarily)
the name that the user sees it as.
Regards,
Malcolm
Okay, that shouldn't be too hard. Assuming you're accepting input
through a form (using Django's form support, in django.forms), you can
write a custom validation/cleaning function for the form field that
checks if the name for the submitted file already exists. Using
os.stat(), for example. If it does exist, you can raise a
ValidationError with an appropriate error message.
Regards,
Malcolm