Django用户保存挂钩

Bha*_*rad 2 django django-admin

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

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

Tim*_*ony 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)
Run Code Online (Sandbox Code Playgroud)

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