Django用户保存挂钩

时间:2012-08-10 22:05:02

标签: django django-admin

在django admin中,当用户被授予超级用户状态时,我希望执行检查。 我想查看用户电子邮件是否来自* .company.com

这样做的最佳方式是什么?

1 个答案:

答案 0 :(得分:5)

创建signal

from django.db.models.signals import post_save
from django.contrib.auth.models import User

def check_superuser(sender, instance, signal, *args, **kwargs):
    if sender is User and instance.is_superuser and not instance.email.endswith('@company.com'):
        ...

post_save.connect(check_superuser, sender=User)

现在,每次保存User的实例时,它都会运行上面的check_superuser方法