修改Django用户删除方法?
如果我进入 Django 管理页面并删除用户,我希望它在删除用户之前/之后运行一些代码。我知道重写模型的 delete()
方法,但我不确定如何将其应用于内置模型。另外,我希望能够在不“子类化”User
模型并创建(例如)MyUser
模型的情况下完成此操作。
有什么办法可以做到这一点吗?
If I go to the Django admin page and delete a user, I want it to run some code before/after it deletes the user. I know about overriding models' delete()
methods, but I'm not sure how to apply it to a built-in model. Also, I'd like to be able to do it without 'subclassing' the User
model and creating a (for instance) MyUser
model.
Is there any way to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,你可以监听 django pre_delete 和 post_delete 信号:
这是文档链接:
http://docs.djangoproject.com /en/dev/ref/signals/#django.db.models.signals.pre_delete
http://docs.djangoproject.com /en/dev/ref/signals/#django.db.models.signals.post_delete
yes, you can listen to the django pre_delete and post_delete signal:
here is the doc link:
http://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.pre_delete
http://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.post_delete