Best way to unique upload file name

142 views
Skip to first unread message

chatchai

unread,
Jan 26, 2009, 11:39:46 PM1/26/09
to Django users
Hi,

I want to unique upload file and there is not unique parameter
available for file field model. I think I can do this at the form
level but I just want to make sure if it is the best way to go.

Any comment or example is welcome.

Thanks,
Chatchai

Malcolm Tredinnick

unread,
Jan 26, 2009, 11:44:49 PM1/26/09
to django...@googlegroups.com
On Mon, 2009-01-26 at 20:39 -0800, chatchai wrote:
> Hi,
>
> I want to unique upload file and there is not unique parameter
> available for file field model. I think I can do this at the form
> level but I just want to make sure if it is the best way to go.

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


chatchai

unread,
Jan 26, 2009, 11:52:06 PM1/26/09
to Django users


On Jan 27, 11:44 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
I know that Django will append the underscore after the file name
until the file can be saved but what i mean is,
do not allow the file with the same name to be able to upload.

Thanks anyway :)

Malcolm Tredinnick

unread,
Jan 26, 2009, 11:57:28 PM1/26/09
to django...@googlegroups.com
On Mon, 2009-01-26 at 20:52 -0800, chatchai wrote:
[...]

> I know that Django will append the underscore after the file name
> until the file can be saved but what i mean is,
> do not allow the file with the same name to be able to upload.

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

chatchai

unread,
Jan 27, 2009, 12:06:11 AM1/27/09
to Django users
Thanks for your quick reply,

I need to unique file upload name because the client say the file is
unique by name (date based) so that they won't upload the same file.

I know that the file name cannot determine file uniqueness.

At this point, I need to return error if they try to upload the same
(name) file.

Thanks and regards,

Chatchai

On Jan 27, 11:57 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:

Malcolm Tredinnick

unread,
Jan 27, 2009, 12:27:05 AM1/27/09
to django...@googlegroups.com
On Mon, 2009-01-26 at 21:06 -0800, chatchai wrote:
> Thanks for your quick reply,
>
> I need to unique file upload name because the client say the file is
> unique by name (date based) so that they won't upload the same file.
>
> I know that the file name cannot determine file uniqueness.
>
> At this point, I need to return error if they try to upload the same
> (name) file.

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


Reply all
Reply to author
Forward
0 new messages