简体   繁体   English

如何将删除原因添加到所有DRF删除API

[英]How to add a Reason for Deletion to all DRF Delete APIs

In a Django Rest Framework app using 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 ? 在使用Django简单历史记录来跟踪模型更改的Django Rest Framework应用程序中,如何迫使用户传递所有销毁端点的 Reason for Deletion ,然后将该原因传递给Django简单历史记录的更改原因

Also, for related models with a Delete Cascade, would that reason be passed on to the related deleted entries? 另外,对于具有删除级联的相关模型,该原因是否会传递到相关的已删除条目?

Update: 更新:

Tried overriding the destroy method as below following another question here on SO . 在SO上的另一个问题之后,尝试覆盖销毁方法,如下所示。 Issue is, where is the changeReason ie history_change_reason located after the delete? 问题是,删除后的changeReason即history_change_reason在哪里? I thought it would be a column in the historical table? 我以为这将是历史记录表中的一列? However, its not there so even if the code below is working, I can't find where the reason has been saved. 但是,它不存在,所以即使下面的代码正常工作,我也找不到原因保存在哪里。

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)

Missing History Change Reason Column: 缺少历史记录更改原因列:

The only history_* I see in all history tables are: 我在所有历史记录表中看到的唯一的history_*是:

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

I can't find history_change_reason in any of the history tables 我在任何历史记录表中都找不到history_change_reason

Ok. 好。 Found the answer to two of my problems. 找到了我两个问题的答案。

Missing History Change Reason Column: 缺少历史记录更改原因列:

This was a version issue. 这是一个版本问题。 pip3.6 install --upgrade django-simple-history and then migrate solved this. pip3.6 install --upgrade django-simple-history ,然后迁移即可解决。

Deletion Reason as a Mixin: 混合原因删除原因:

This works by checking if deletion_reason is provided. 这通过检查是否提供了delete_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)

Not solved: 未解决:

Passing the deletion_reason to all tables that are subsequently deleted when you have a delete_cascade in the relation between models. 路过deletion_reason当你在模型之间的关系的delete_cascade其随后删除所有表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM