[Django] #28428: Add support for Pathlib objects in django.core.storage

27 views
Skip to first unread message

Django

unread,
Jul 21, 2017, 7:06:51 PM7/21/17
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.storage
-----------------------------------------+------------------------
Reporter: Tom | Owner: nobody
Type: New feature | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
Code in django.core.storage will explode when given a Pathlib object on
Python 3.4 and 3.5. For example, this throws a cryptic `AttributeError:
'PosixPath' object has no attribute 'rfind'` exception due to
`generate_filename` only expecting strings:


{{{
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.

Django

unread,
Jul 21, 2017, 8:23:20 PM7/21/17
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.storage
-------------------------------------+-------------------------------------

Reporter: Tom | Owner: nobody
Type: New feature | Status: new
Component: File | Version: master
uploads/storage |
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* 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>

Django

unread,
Jul 22, 2017, 7:25:55 AM7/22/17
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.storage
-------------------------------------+-------------------------------------

Reporter: Tom | Owner: nobody
Type: New feature | Status: new
Component: File | Version: master
uploads/storage |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jul 24, 2017, 9:22:24 AM7/24/17
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.storage
-------------------------------------+-------------------------------------

Reporter: Tom | Owner: nobody
Type: New feature | Status: new
Component: File | Version: master
uploads/storage |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* 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>

Django

unread,
Oct 30, 2017, 8:53:00 AM10/30/17
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.storage
-------------------------------------+-------------------------------------
Reporter: Tom Forbes | Owner: nobody

Type: New feature | Status: new
Component: File | Version: master
uploads/storage |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Sep 6, 2018, 11:57:20 AM9/6/18
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.storage
-------------------------------------+-------------------------------------
Reporter: Tom Forbes | Owner: nobody

Type: New feature | Status: new
Component: File | Version: master
uploads/storage |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Aug 15, 2019, 12:38:32 PM8/15/19
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.storage
--------------------------------------+------------------------------------
Reporter: Tom Forbes | Owner: nobody

Type: New feature | Status: new
Component: File uploads/storage | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Claude Paroz):

* 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>

Django

unread,
Aug 18, 2019, 2:53:34 PM8/18/19
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.storage
--------------------------------------+------------------------------------
Reporter: Tom Forbes | Owner: nobody

Type: New feature | Status: new
Component: File uploads/storage | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

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>

Django

unread,
Aug 18, 2019, 2:53:34 PM8/18/19
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.storage
--------------------------------------+------------------------------------
Reporter: Tom Forbes | Owner: nobody

Type: New feature | Status: new
Component: File uploads/storage | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

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>

Django

unread,
Aug 18, 2019, 3:58:10 PM8/18/19
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.storage
--------------------------------------+------------------------------------
Reporter: Tom Forbes | Owner: nobody

Type: New feature | Status: new
Component: File uploads/storage | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by felixxm):

* has_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:9>

Django

unread,
Oct 30, 2019, 8:36:19 AM10/30/19
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.storage
--------------------------------------+------------------------------------
Reporter: Tom Forbes | Owner: nobody

Type: New feature | Status: new
Component: File uploads/storage | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

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>

Django

unread,
Oct 31, 2019, 4:46:44 AM10/31/19
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.storage
--------------------------------------+------------------------------------
Reporter: Tom Forbes | Owner: nobody

Type: New feature | Status: new
Component: File uploads/storage | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

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>

Django

unread,
Oct 31, 2019, 4:46:45 AM10/31/19
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.storage
--------------------------------------+------------------------------------
Reporter: Tom Forbes | Owner: nobody

Type: New feature | Status: new
Component: File uploads/storage | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

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>

Django

unread,
Sep 10, 2020, 4:58:25 AM9/10/20
to django-...@googlegroups.com
#28428: Add support for Pathlib objects in django.core.files.storage
--------------------------------------+------------------------------------
Reporter: Tom Forbes | Owner: nobody
Type: New feature | Status: closed

Component: File uploads/storage | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => fixed


Comment:

All `FileSystemStorage`'s methods now support `pathlib.Path()`.

--
Ticket URL: <https://code.djangoproject.com/ticket/28428#comment:13>

Reply all
Reply to author
Forward
0 new messages