This is a very beginner question. But I'm stumped. How do I reference a django settings variable in my model.py?
这是一个初学者的问题。但我难住了。如何在模型.py中引用django设置变量?
NameError: name 'PRIVATE_DIR' is not defined Also tried a lot of other stuff including settings.PRIVATE_DIR
NameError:没有定义名称“PRIVATE_DIR”还尝试了许多其他的东西,包括settings.PRIVATE_DIR
What's the correct way to do this?
正确的做法是什么?
---------settings.py-------------
- - - - - - - - - - -settings.py - - - - - - - - - - - - -
PRIVATE_DIR = '/home/me/django_projects/myproject/storage_dir'
---------model.py---------------
- - - - - - - - - - -model.py - - - - - - - - - - - - - - - -
# Problem is here.
from django.core.files.storage import FileSystemStorage
fs = FileSystemStorage(location=PRIVATE_DIR)
class Customer(models.Model):
lastName = models.CharField(max_length=20)
firstName = models.CharField(max_length=20)
image = models.ImageField(storage=fs, upload_to='photos', blank=True, null=True)
2 个解决方案
#1
159
Try with this: from django.conf import settings
试试这个:来自django。配置导入设置
#2
60
from django.conf import settings
PRIVATE_DIR = getattr(settings, "PRIVATE_DIR", None)
Where it says None
, you will put a default value incase the variable isn't defined in settings.
如果它说无,则在设置中未定义变量时,将设置一个默认值。
#1
159
Try with this: from django.conf import settings
试试这个:来自django。配置导入设置
#2
60
from django.conf import settings
PRIVATE_DIR = getattr(settings, "PRIVATE_DIR", None)
Where it says None
, you will put a default value incase the variable isn't defined in settings.
如果它说无,则在设置中未定义变量时,将设置一个默认值。