#32718: [3.2.1] Issue with assigning file to FileField
-------------------------------------+-------------------------------------
     Reporter:  Jakub Kleň           |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  2.2
  (models, ORM)                      |
     Severity:  Release blocker      |               Resolution:
     Keywords:  3.2.1 file model     |             Triage Stage:  Accepted
  filefield fieldfile                |
    Has patch:  1                    |      Needs documentation:  1
  Needs tests:  1                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Jakub Kleň):

 I'm still not completely sure if we should disallow absolute paths in the
 `File`. When I'm for example setting a file from `/tmp`, which I'm doing
 in my project, it would force me to override the `File.name` like this:
 {{{
 file = File(open('/tmp/image.png', 'rb'), name='image.png')
 # which would then break:
 file.close()
 file.open()
 }}}

 I know that the `open` method is not called when using the `File` to
 update a model, but it doesn't seem to be the right thing to do.

 Should we be using the path from `File.name` and append it to `upload_to`?
 In my project, I use a callable `upload_to`, which takes care of the path
 and filename, and only takes the extension of the original filename. Isn't
 that a better solution if we want a custom path that depends on the model
 instance?

 {{{
 def custom_upload_to(instance, filename):
     extension = os.path.splitext(filename)[1][1:].lower()
     actual_filename = 'image.' + extension
     return os.path.join('users', instance.username, actual_filename)

 model.file = File(open('/tmp/sth.png', 'rb'))
 }}}

 My point here is, shouldn't we be using `upload_to` for the purpose of
 constructing a custom file path?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32718#comment:28>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.7618142f246a9bb0a1f31989cffbd572%40djangoproject.com.

Reply via email to