Opened 4 years ago

Closed 5 months ago

#28428 closed New feature (fixed)

Add support for Pathlib objects in

Reported by: Tom Forbes Owned by: nobody
Component: File uploads/storage Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Code in 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.

Change History (13)

comment:1 Changed 4 years ago by Tim Graham

Component: UncategorizedFile uploads/storage

Do you have a use case in mind? What are the advantages?

comment:2 Changed 4 years ago by Tom Forbes

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.

comment:3 Changed 4 years ago by Tim Graham

Triage Stage: UnreviewedSomeday/Maybe

I'm not sure. I guess I'd have to see a patch to evaluate the idea.

comment:4 Changed 3 years ago by Roger G. Coram

As of Python 3.6 (specifically running 3.6.3) 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')

comment:5 Changed 2 years ago 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.

comment:6 Changed 18 months ago by Claude Paroz

Has patch: set
Triage Stage: Someday/MaybeAccepted


It's easier now that Python 3.6 is the minimal version.

comment:7 Changed 18 months ago by Mariusz Felisiak <felisiak.mariusz@…>

In af69842d:

Refs #28428 -- Added test for a callable FileField.upload_to that returns pathlib.Path.

comment:8 Changed 18 months ago by Mariusz Felisiak <felisiak.mariusz@…>

In d1c2e6dd:

Refs #28428 -- Made FileField.upload_to support pathlib.Path.

comment:9 Changed 18 months ago by Mariusz Felisiak

Has patch: unset

comment:10 Changed 15 months ago by Mariusz Felisiak <felisiak.mariusz@…>

In 6315a27:

Refs #28428 -- Made filepath_to_uri() support pathlib.Path.

comment:11 Changed 15 months ago by Mariusz Felisiak <felisiak.mariusz@…>

In 0f297553:

Refs #28428 -- Added tests for using FileSystemStorage with pathlib.Path.

comment:12 Changed 15 months ago by Mariusz Felisiak <felisiak.mariusz@…>

In 17752003:

Refs #28428 -- Made to support pathlib.Path.

comment:13 Changed 5 months ago by Mariusz Felisiak

Resolution: fixed
Status: newclosed
Summary: Add support for Pathlib objects in django.core.storageAdd support for Pathlib objects in

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

Note: See TracTickets for help on using tickets.
Back to Top