在 django settings.py 中,資料庫默認為:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
但是當我在 python中執行時a={'a':'a'/'b'},我收到錯誤 TypeError: unsupported operand type(s) for /: 'str' and 'str'。為什么錯誤沒有出現在 django 中?
我想在子檔案夾中為我的資料庫定義不同的路徑,以便 django 自動創建子檔案夾和 sqlite 資料庫......我該怎么做?
謝謝你
uj5u.com熱心網友回復:
但是當我在 python中做時
a={'a':'a'/'b'},我得到了錯誤TypeError: unsupported operand type(s) for /: 'str' and 'str'。為什么錯誤沒有出現在 django 中?
因為BASE_DIR不是string,而是Path物件 [Python-doc]。對于這個物件,__div__已經定義了方法,因此它some_path / 'foo.sqlite3'用于將左側的路徑與右側的字串連接起來。確實,例如:
>>> Path('/etc') / 'foo' / 'bar.data'
PosixPath('/etc/foo/bar.data')
我想在子檔案夾中為我的資料庫定義不同的路徑,以便 django 自動創建子檔案夾和 sqlite 資料庫......我該怎么做?
您可以指定不同的路徑,例如:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'subdir' / 'db.sqlite3',
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/482772.html
上一篇:在模型之間實作雙向導航屬性
