1,在app下创建signals.py
from django.contrib.auth import get_user_model
from django.db.models.signals import post_save
from django.dispatch import receiver
User = get_user_model()
@receiver(post_save, sender=User)
def create_user(sender, instance=None, created=False, **kwargs):
if created:
password = instance.password
instance.set_password(password)
instance.save()
2, signal接受函数需要确保将此代码段放置在已安装的models.py模块或Django在启动时导入的其他位置
加载signals设置: apps/users/init.py中填写:
default_app_config = 'users.apps.UsersConfig'
3,随后在apps/user/app.py的config类下重写ready方法:
from django.apps import AppConfig
class UsersConfig(AppConfig):
pass
def ready(self):
import users.signals
django信号量
最新推荐文章于 2023-12-25 23:10:49 发布