3👍
pathlib.Path
s are not strings (or bytes). Most internal Django code uses the os.path
functions, and those require strings/bytes, and code that expects a string (like it looks like database_name
is expecting) cannot work with pathlib.Path
objects — you’ll need to convert it to string (ie. str(BASE_DIR.joinpath('db.sqlite3')
It’s possible to write a Path class that is a subclass of str
, which makes interaction with code that expects string much more transparent (many have created such classes, including me: https://github.com/datakortet/dkfileutils/blob/master/dkfileutils/path.py).
1👍
Your use case can even be a little simpler:
BASE_DIR = Path.cwd()
DATABASE.NAME = str(BASE_DIR / "db.sqlite3")
Note: The conversion to a string, because Django can’t yet handle Pathlib instances.
1👍
pathlib was added to Python’s standard library in Python 3.4, thanks to PEP 428 by the way. All file-path using functions across Python were then enhanced to support pathlib.Path objects (or anything with a fspath method) in Python 3.6, thanks to PEP 519.
For example, if you are using the SQLite database backend, before opening the database file it checks if the path contains "mode=memory". For this to work, you’ll need to use str() when passing it the path in NAME:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": str(BASE_DIR / "db.sqlite3"),
}
}