尽管一切就绪,Django 仍然发出信号不工作
Django signals not working inspite of putting everything in place
我的案例
在我的例子中,用户是默认的 django 用户模型,我创建了一个配置文件来向用户模型添加更多详细信息。
实现
现在我想要的是,每当我创建一个新用户时,应该自动为该用户创建一个配置文件。
我完成了
1. 我检查了我的 signals.py 文件并将信号导入 apps.py 文件,但仍然没有为每个正在创建的新用户创建 nw 配置文件:(
2. 尝试将 'users.apps.UsersConfig' 添加到我的 INSTALLED_APPS 中,但也没有成功。
代码如下
我在下面的 signals.py 和 apps.py 文件中提供了代码。
请问我是否需要更多代码
提前致谢:)
这是我的 signals.py 文件
#This is the signal that will be sent
from django.db.models.signals import post_save
#This is the object which will send the signal
from django.contrib.auth.models import User
#This will receive the signal
from django.dispatch import receiver
#We need this to perform operations on profiles table
from .models import Profile
#This function creates new profile for each user created
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
#This function saves those newly created profiles
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
这是我的 apps.py 文件
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
def ready(self):
import users.signals
这是我的 INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#Custom Apps
'product',
'shop',
'market',
'pages',
'users',
]
我在 INSTALLED_APPS 中使用 'users' 而不是 'users.apps.UsersConfig'。
我的新作品 INSTALLED_APPS 看起来像这样:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#Custom Apps
'product',
'shop',
'market',
'pages',
'users.apps.UsersConfig', #Signals wont work if you just write 'users'
]
或者只是参考一个类似的问题:
我的案例
在我的例子中,用户是默认的 django 用户模型,我创建了一个配置文件来向用户模型添加更多详细信息。
实现
现在我想要的是,每当我创建一个新用户时,应该自动为该用户创建一个配置文件。
我完成了
1. 我检查了我的 signals.py 文件并将信号导入 apps.py 文件,但仍然没有为每个正在创建的新用户创建 nw 配置文件:(
2. 尝试将 'users.apps.UsersConfig' 添加到我的 INSTALLED_APPS 中,但也没有成功。
代码如下
我在下面的 signals.py 和 apps.py 文件中提供了代码。
请问我是否需要更多代码
提前致谢:)
这是我的 signals.py 文件
#This is the signal that will be sent
from django.db.models.signals import post_save
#This is the object which will send the signal
from django.contrib.auth.models import User
#This will receive the signal
from django.dispatch import receiver
#We need this to perform operations on profiles table
from .models import Profile
#This function creates new profile for each user created
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
#This function saves those newly created profiles
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
这是我的 apps.py 文件
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
def ready(self):
import users.signals
这是我的 INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#Custom Apps
'product',
'shop',
'market',
'pages',
'users',
]
我在 INSTALLED_APPS 中使用 'users' 而不是 'users.apps.UsersConfig'。
我的新作品 INSTALLED_APPS 看起来像这样:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#Custom Apps
'product',
'shop',
'market',
'pages',
'users.apps.UsersConfig', #Signals wont work if you just write 'users'
]
或者只是参考一个类似的问题: