在 django 管理中,当用户被赋予 super 用户状态时,我希望执行检查。 我想查看用户电子邮件是否来自 *.company.com 形式
最好的方法是什么?
最佳答案
创建 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
方法
关于Django 用户保存钩子(Hook),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11909877/