如何向所有 DRF 删除 API 添加删除原因
How to add a Reason for Deletion to all DRF Delete APIs
在 Django Rest Framework 应用程序中使用 Django Simple History to track model changes, how would one force a user to pass a Reason for Deletion
for all Destroy End Points and then pass that reason to Django Simple History's Change Reason?
此外,对于具有 Delete Cascade 的相关模型,该原因是否会传递给相关的已删除条目?
更新:
在 之后尝试重写 destroy 方法,如下所示。问题是,删除后的 changeReason 即 history_change_reason
在哪里?我以为它会是历史上的一个专栏table?但是,它不存在,所以即使下面的代码有效,我也找不到保存原因的位置。
class DeleteViewSet(mixins.DestroyModelMixin):
def destroy(self, request, *args, **kwargs):
try:
if 'delete_reason' not in request.data.keys():
return Response(status=status.HTTP_400_BAD_REQUEST,data='{delete_reason: Invalid Delete Reason}')
else:
instance = self.get_object()
instance.changeReason = request.data['delete_reason']
instance.save()
self.perform_destroy(instance)
except Http404:
pass
return Response(status=status.HTTP_204_NO_CONTENT)
缺少历史更改原因列:
我在所有历史记录中看到的唯一 history_*
table 是:
1. history_id
2. history_date
3. history_type
4. history_user_id
我在任何历史记录中都找不到 history_change_reason
tables
好的。找到了我的两个问题的答案。
缺少历史更改原因列:
这是一个版本问题。 pip3.6 install --upgrade django-simple-history
然后迁移解决了这个问题。
作为混入的删除原因:
这通过检查是否提供了 deletion_reason 来实现。
class DeleteViewSet(mixins.DestroyModelMixin):
def destroy(self, request, *args, **kwargs):
try:
if 'delete_reason' not in request.data.keys():
return Response(status=status.HTTP_400_BAD_REQUEST,data='{delete_reason: Invalid Delete Reason}')
else:
instance = self.get_object()
instance.changeReason = request.data['delete_reason']
# instance.save() ==>Don't do this because it will cause a second instance to be saved in the history tables
self.perform_destroy(instance)
except Http404:
pass
return Response(status=status.HTTP_204_NO_CONTENT)
未解决:
将 deletion_reason
传递给所有表,当您在模型之间的关系中有 delete_cascade 时,这些表随后将被删除。
在 Django Rest Framework 应用程序中使用 Django Simple History to track model changes, how would one force a user to pass a Reason for Deletion
for all Destroy End Points and then pass that reason to Django Simple History's Change Reason?
此外,对于具有 Delete Cascade 的相关模型,该原因是否会传递给相关的已删除条目?
更新:
在 history_change_reason
在哪里?我以为它会是历史上的一个专栏table?但是,它不存在,所以即使下面的代码有效,我也找不到保存原因的位置。
class DeleteViewSet(mixins.DestroyModelMixin):
def destroy(self, request, *args, **kwargs):
try:
if 'delete_reason' not in request.data.keys():
return Response(status=status.HTTP_400_BAD_REQUEST,data='{delete_reason: Invalid Delete Reason}')
else:
instance = self.get_object()
instance.changeReason = request.data['delete_reason']
instance.save()
self.perform_destroy(instance)
except Http404:
pass
return Response(status=status.HTTP_204_NO_CONTENT)
缺少历史更改原因列:
我在所有历史记录中看到的唯一 history_*
table 是:
1. history_id
2. history_date
3. history_type
4. history_user_id
我在任何历史记录中都找不到 history_change_reason
tables
好的。找到了我的两个问题的答案。
缺少历史更改原因列:
这是一个版本问题。 pip3.6 install --upgrade django-simple-history
然后迁移解决了这个问题。
作为混入的删除原因:
这通过检查是否提供了 deletion_reason 来实现。
class DeleteViewSet(mixins.DestroyModelMixin):
def destroy(self, request, *args, **kwargs):
try:
if 'delete_reason' not in request.data.keys():
return Response(status=status.HTTP_400_BAD_REQUEST,data='{delete_reason: Invalid Delete Reason}')
else:
instance = self.get_object()
instance.changeReason = request.data['delete_reason']
# instance.save() ==>Don't do this because it will cause a second instance to be saved in the history tables
self.perform_destroy(instance)
except Http404:
pass
return Response(status=status.HTTP_204_NO_CONTENT)
未解决:
将 deletion_reason
传递给所有表,当您在模型之间的关系中有 delete_cascade 时,这些表随后将被删除。