#4345 closed Bug (fixed)
FileField cannot be used with unique=True
| Reported by: | Owned by: | Marty Alchin | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Normal | Keywords: | fs-rf-fixed |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
The following model will result in an error in the (current) admin:
class Foo(Model): name = CharField(maxlength=50) file = FileField(upload_to="upload", unique=True)
When trying to add a Foo object in the admin, the following error occurs:
ProgrammingError at /admin/browser/documentfile/add/ can't adapt Request Method: POST Request URL: http://localhost:8000/admin/browser/documentfile/add/ Exception Type: ProgrammingError Exception Value: can't adapt Exception Location: /django_src/django/db/backends/util.py in execute, line 12
Removing unique=True from the file field makes it work -- but what will happen if a file with the same name is uploaded twice, attached to different objects? What will happen when either one is removed?
Change History (11)
comment:1 by , 18 years ago
| Component: | Database wrapper → Documentation |
|---|---|
| Owner: | changed from to |
comment:2 by , 18 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
Well it shouldn't be erroring, so let's accept this as a valid bug.
Perhaps FileField just shouldn't accept the unique=True argument.
comment:3 by , 18 years ago
Just as a side note: http://www.djangoproject.com/documentation/db-api/ actually documents this:
save_FOO_file(filename, raw_contents)
"For every FileField, the object will have a save_FOO_file() method, where FOO is the name of the field. This saves the given file to the filesystem, using the given filename. If a file with the given filename already exists, Django adds an underscore to the end of the filename (but before the extension) until the filename is available."
Could be mentioned in the FileField docs as well, though.
comment:4 by , 18 years ago
| Component: | Documentation → Database wrapper |
|---|---|
| Owner: | changed from to |
This isn't a doc bug; it's a model API bug. Actually, though, I'm not clear on what a "unique" file would be -- do we have do examine each uploaded file against all the others? That's crazy talk, I think... Perhaps just disallowing unique on FileFields would do the trick.
comment:5 by , 18 years ago
| Keywords: | fs-rf added |
|---|
comment:6 by , 18 years ago
| Keywords: | fs-rf-fixed added; fs-rf removed |
|---|
comment:7 by , 17 years ago
| milestone: | → 1.0 beta |
|---|
comment:8 by , 17 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:9 by , 17 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
(In [8244]) File storage refactoring, adding far more flexibility to Django's file handling. The new files.txt document has details of the new features.
This is a backwards-incompatible change; consult BackwardsIncompatibleChanges for details.
Fixes #3567, #3621, #4345, #5361, #5655, #7415.
Many thanks to Marty Alchin who did the vast majority of this work.
comment:12 by , 9 years ago
| Easy pickings: | unset |
|---|---|
| Severity: | → Normal |
| Type: | → Bug |
| UI/UX: | unset |
#27188 is a follow up ticket that argues to allow using unique=True with FileField.
Hmm...
Apparently, what happens is this:
If the file already exists, the new file will be named
<base name><_>.<suffix>, where<_>is the number of underscores needed to make the name unique. So, for example, when a file namedfoo.pngis uploaded multiple times, the second file will be namedfoo_.png, the third will befoo__.pngand 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.