#27188: Allow using unique=True with FileField
-------------------------------------+-------------------------------------
     Reporter:  Jon Ribbens          |                    Owner:  Michael
                                     |  Scott
         Type:  New feature          |                   Status:  assigned
    Component:  File                 |                  Version:  1.10
  uploads/storage                    |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  1                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Michael Scott):

 I did a little testing over the weekend on the behaviour of FileField's
 after the patch has been applied. In general things behaved as you would
 expect them to, when unique is set to True then it doesn't allow you to
 save more than one instance of the model when a duplicate file name is
 used for the FileField field.

 However there is a bit of unexpected behaviour when using unique
 FileField's across separate models. If the same directory is used, which
 by default is the MEDIA_ROOT setting, to store the files from the multiple
 models, then the unique constraint may have no effect. This is because
 when the (default) storage object saves the file, it checks if the
 destination directory already contains a file with the specified name, and
 if so it generates a unique file name instead (appends a unique suffix to
 the end). In this case that generated file name is what's stored in the
 database and therefore as the generated file name is unique the constraint
 has no effect.

 For example, with models ModelFoo and ModelBar, each having a FileField
 with unique set to True. If saving an instance of ModelFoo with file name
 "example.txt", the first time will succeed, then if trying to save another
 instance of ModelFoo with the same file,  the second time will fail as
 expected because the table already has a entry in it with that file name.
 However if then saving an instance of ModelBar with the same name, then
 the storage object generates a unique file name such as
 "example_abfgr.txt" as "example.txt" already exists in the directory, and
 saves the ModelBar instance with that name. Then when saving another
 instance of ModelBar with the same file, the same thing happens again,
 meaning the unique constraint has no effect.

 I hope this all makes sense. Is the above behaviour something we need to
 worry about?

--
Ticket URL: <https://code.djangoproject.com/ticket/27188#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.5b0f2e7c2b12aff77a3cbe660f1740a9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to