5
我希望管理員在某些對象被刪除時得到通知,但我也想確定哪個用戶正在執行刪除操作。Django:確定哪個用戶在使用post_delete信號時正在刪除
可能嗎?
這是代碼:
# models.py
# signal to notify admins when nodes are deleted
from django.db.models.signals import post_delete
from settings import DEBUG
def notify_on_delete(sender, instance, using, **kwargs):
''' Notify admins when nodes are deleted. Only for production use '''
if DEBUG:
#return False
pass
# prepare context
context = {
'node': instance,
'site': SITE
}
# notify admins that want to receive notifications
notify_admins(instance, 'email_notifications/node-deleted-admin_subject.txt', 'email_notifications/node-deleted-admin_body.txt', context, skip=False)
post_delete.connect(notify_on_delete, sender=Node)
豈不是如果信號是由模型而不是發射了更好視圖?因此,管理界面和第三方代碼仍然可以工作? –
@Jack M.我想這樣會更好,但是你怎麼知道哪個'User'在沒有'request'的情況下做了刪除? –