{{{
def upload_to(filename):
return pathlib.Path(filename)
class Storage(models.Model):
file = models.FileField(upload_to=upload_to)
}}}
More generally, it would be nice if all methods in the django.core.files
accepted pathlib objects, and used pathlib objects internally. For
compatibility reasons I think strings would have to be returned.
--
Ticket URL: <https://code.djangoproject.com/ticket/28428>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* component: Uncategorized => File uploads/storage
Comment:
Do you have a use case in mind? What are the advantages?
--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:1>
Comment (by Tom):
In my case we have a bunch of methods in our applications that all use
pathlib instances, some of which is used by our upload_to methods. It
feels a bit strange to have to return the string representation from them
when interacting with Django. With Python 2 support out of the picture
there isn't much reason to, other than historical ones.
The main advantage is readability, in a lot of cases using Pathlib makes
`os.path` heavy code a lot less dense.
--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:2>
* stage: Unreviewed => Someday/Maybe
Comment:
I'm not sure. I guess I'd have to see a patch to evaluate the idea.
--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:3>
Comment (by Roger G. Coram):
As of Python 3.6 (specifically running 3.6.3)
`django.core.files.storage.Storage.generate_filename` doesn't appear to
raise this error when passed a `pathlib.Path`. Instead the `os.path.split`
therein appears to handle it as expected:
{{{
Python 3.6.3 (default, Oct 6 2017, 08:44:35)
>>> import os, pathlib
>>> os.path.split(pathlib.PosixPath("some/folder/test_with_space.txt"))
('some/folder', 'test_with_space.txt')
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:4>
Comment (by Kyle Agronick):
Pathlib provides a much nicer object oriented syntax rather than dealing
with strings. os.path supports pathlib. Django really should too. At the
very least all you would have to do is cast to a string and everything
else would work as normal.
--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:5>
* has_patch: 0 => 1
* stage: Someday/Maybe => Accepted
Comment:
[https://github.com/django/django/pull/11674 PR].
It's easier now that Python 3.6 is the minimal version.
--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:6>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"d1c2e6dd04d4ddc8ace17e6abfed16ac46459d1e" d1c2e6dd]:
{{{
#!CommitTicketReference repository=""
revision="d1c2e6dd04d4ddc8ace17e6abfed16ac46459d1e"
Refs #28428 -- Made FileField.upload_to support pathlib.Path.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:8>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"af69842dbd98e69519f1263eca2619c3165ba13b" af69842d]:
{{{
#!CommitTicketReference repository=""
revision="af69842dbd98e69519f1263eca2619c3165ba13b"
Refs #28428 -- Added test for a callable FileField.upload_to that returns
pathlib.Path.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:7>
* has_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:9>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"6315a272c5cfcc70b745f87b25148a158663a222" 6315a27]:
{{{
#!CommitTicketReference repository=""
revision="6315a272c5cfcc70b745f87b25148a158663a222"
Refs #28428 -- Made filepath_to_uri() support pathlib.Path.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:10>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"17752003a8c115ff79f5f21655f5e9b8b2af67f4" 17752003]:
{{{
#!CommitTicketReference repository=""
revision="17752003a8c115ff79f5f21655f5e9b8b2af67f4"
Refs #28428 -- Made FileSystemStorage.save() to support pathlib.Path.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:12>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"0f2975534dc6a010a4da28165b235f36bf1df24f" 0f297553]:
{{{
#!CommitTicketReference repository=""
revision="0f2975534dc6a010a4da28165b235f36bf1df24f"
Refs #28428 -- Added tests for using FileSystemStorage with pathlib.Path.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:11>
* status: new => closed
* resolution: => fixed
Comment:
All `FileSystemStorage`'s methods now support `pathlib.Path()`.
--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:13>