2

在 django 管理员中,当用户获得超级用户身份时,我希望执行检查。我想查看用户电子邮件是否来自 *.company.com 的形式

做这个的最好方式是什么?

4

1 回答 1

5

创建一个信号

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方法

于 2012-08-10T22:10:45.580 回答