我有一个 View 集,我只想将限制应用于 create
该 View 集的操作,我不希望它应用于 update
, destroy
, retrieve
等等...
class UserViewSet(viewsets.ModelViewSet):
# should only be applied to the create action
throttle_classes = [SomeThrottle]
...
最佳答案
如 Introspecting ViewSet actions [DRF docs] 中所述您可以查看 action
属性设置 throttle_classes
通过覆盖 get_throttles
基于当前操作方法:
class UserViewSet(viewsets.ModelViewSet):
def get_throttles(self):
if self.action == 'create':
throttle_classes = [SomeThrottle]
else:
throttle_classes = [] # No throttle for other actions
return [throttle() for throttle in throttle_classes]
关于django - 如何应用节流只是为了在 DRF View 集中创建 Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69068513/