0

在使用Django Simple History跟踪模型更改的 Django Rest Framework 应用程序中,如何强制用户Reason for Deletion为所有Destroy End Points传递 a ,然后将该原因传递给 Django Simple History 的Change Reason

此外,对于具有删除级联的相关模型,该原因是否会传递给相关的已删除条目?

更新:

在 SO 上的另一个问题之后,尝试如下覆盖 destroy 方法。问题是,changeReason 即history_change_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()
                self.perform_destroy(instance)
        except Http404:
            pass
        return Response(status=status.HTTP_204_NO_CONTENT)

缺少历史更改原因列:

history_*我在所有历史表中看到的唯一内容是:

1. history_id
2. history_date
3. history_type
4. history_user_id

history_change_reason我在任何历史记录表中都找不到

4

1 回答 1

0

好的。找到了我的两个问题的答案。

缺少历史更改原因列:

这是一个版本问题。pip3.6 install --upgrade django-simple-history然后迁移解决了这个问题。

作为 Mixin 的删除原因:

这通过检查是否提供了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 时,将传递给随后被删除的所有表。

于 2017-07-08T08:41:26.667 回答