我使用的是Azure后端django-storage。我想在本地运行时使用本地提供的Django解决方案,在生产中运行时使用Azure存储。我该如何根据设置来改变它?我只是在下面使用IF语句吗?
# local
image = models.ImageField(upload_to="profile_pics", blank=True, null=True)
# production
image = models.ImageField(upload_to="profile_pics", blank=True, null=True, storage=AzureMediaStorage())
您可以在这里的settings.py中设置默认存储,并为生产或调试环境更改它。这将改变整个应用程序的存储空间。
如果您只想为某些字段设置存储空间,您可以使用可调用对象:
from django.conf import settings
from django.db import models
from .storages import MyLocalStorage, MyRemoteStorage
from django.core.files.storage import default_storage
def select_storage():
return default_storage if settings.DEBUG else AzureMediaStorage()
class MyModel(models.Model):
image = models.ImageField(upload_to="profile_pics", blank=True, null=True, storage=select_storage)