我在工作中使用DRF构建面向客户的Web应用程序。我们有一个配置文件模型,其中包含一个ImageField
,分别代表一个用户配置文件和他的配置文件图片。我的工作是停止使用FileSystem作为存储,并切换到使用s3存储图像。我认为我可以使用django-storages
应用来做到这一点。但是我不明白如何让django-storages
只能在模型中的一个字段上使用,而不是将其用作默认存储。我尝试使用FileField
设置为storage
的{{1}},但是无法将文件保存到s3。
总结我的问题-
- 如何将
storages.backends.s3boto3.S3Boto3Storage
配置为仅一个字段的存储空间 - 如何在模型中设置和检索字段。
django-storages
class Profile(models.Model):
img = ImageField(upload_to=get_directory,default='img/generic_profile.jpg')
name = CharField(max_length=256)
new_s3_field = FileField(storage=S3Boto3Storage)
profile = Profile()
profile.new_s3_field = ContentFile('sample.jpg',open('sampel.jpg') # does not work