我已将 django REST API 设置为在调试模式下使用本地存储,在生产环境中使用 S3 存储。这适用于公共文件,因为我覆盖了DEFAULT_FILE_STORAGE这样的:
if IS_DEBUG:
DEFAULT_FILE_STORAGE = 'api.storage_backends.PublicMediaStorage'
Run Code Online (Sandbox Code Playgroud)
每个人都会FileField自动使用它。现在我想以同样的方式使用私有 S3 存储,但是因为我必须显式定义存储 ( FileField(storage=PrivateMediaStorage())),所以总是使用 S3 存储。
在调试模式下如何使用本地存储而不是 S3 存储?
PS:我已经考虑过FileField根据 DEBUG 模式将模型更改为使用或不使用显式存储。这并没有完全解决我的问题,因为我的迁移是在 DEBUG 模式下创建的,因此始终包含没有私有存储类的模型。
更新:
我正在寻找一种解决方案,它可以在两种环境中共享相同的迁移,并且仅在运行时延迟实例化实际存储类。就像 djangoDEFAULT_FILE_STORAGE已经处理了一样。
python django amazon-s3 django-storage django-rest-framework
我有一个 Django 模型Donation,我将其公开为 ViewSet。现在我想向第二个模型添加一个额外的 URL ,其中可以通过参数检索Shop相关实例,并且可以执行自定义操作。Donationorder_id
# models.py
class Donation(models.Model):
id = models.AutoField(primary_key=True)
order_id = models.StringField(help_text='Only unique in combination with field `origin`')
origin = models.ForeignKey('Shop', on_delete=models.PROTECT)
class Shop(models.Model):
id = models.AutoField(primary_key=True)
# views.py
class DonationViewSet(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
def retrieve(self, request, *args, **kwargs):
if kwargs['pk'].isdigit():
return super(DonationViewSet, self).retrieve(request, *args, **kwargs)
else:
shop_id = self.request.query_params.get('shop_id', None)
order_id = self.request.query_params.get('order_id', None)
if shop_id is not None and order_id is not None:
instance = Donations.objects.filter(origin=shop_id, order_id=order_id).first()
if …Run Code Online (Sandbox Code Playgroud) 使用 DRF 记录 API 的内置方式,我能够编写如下所示的文档字符串,并且每个操作都由其相应的行记录:
"""
list: The list action returns all available objects.
retrieve:The retrieve action returns a single object selected by `id`.
create: The create action expects the fields `name`, creates a new object and returns it.
"""
Run Code Online (Sandbox Code Playgroud)
我正在切换到该库drf-spectacular,它可以轻松生成符合 OpenAPI 的方案。然而,现在为每个操作呈现相同的文档字符串,这使得我的文档非常长且冗余。
有没有办法只为每个操作呈现文档字符串的相关部分?