将 users.signals 导入用户应用程序中的 apps.py 时出现 Django 错误
django error in import users.signals into apps.py in users app
以下是我的 settings.py 安装的应用程序部分
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
# third party
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'allauth.socialaccount',
'rest_auth.registration',
'corsheaders',
'crispy_forms',
# local apps
'users.apps.UsersConfig',
]
AUTH_USER_MODEL = 'users.CustomUser'
以下是我的apps.py文件内容
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
def ready(self):
import users.signals
以下是我的signal.py文件内容
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import Profile
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
init.py 用户应用程序中的文件为空
当我 运行 命令 python manage.py makemigrations 它给出错误 ModuleNotFoundError: No module named 'users.users'
并且它还在 apps.py 的以下行中显示错误“No module named users”
导入 users.signals
我的项目目录结构截图如附图
[在此处输入图片描述][1]
[1]: https://i.stack.imgur.com/PNU7p.png
参考上面的 django configuration/code 需要更正什么来解决 apps.py 中的错误,这样就不会解析名为 users 的模块,并为用户应用程序正确加载信号
编辑:根据其他用户提供解决方案的需要,我当前正在开发的自定义用户模型如下
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import ugettext_lazy as _
from .managers import CustomUserManager
class CustomUser(AbstractUser):
CHOICES = (
('T', 'Teacher'),
('I', 'Institute'),
('S', 'Student'),
)
role = models.CharField(max_length=1, choices=CHOICES)
email = models.EmailField(_('email address'), unique=True)
date_of_birth = models.DateField(blank=True, null=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = CustomUserManager()
def __str__(self):
return self.email
class Student(models.Model):
user_student = models.OneToOneField(
CustomUser, on_delete=models.CASCADE, related_name="student_account"
)
standard = models.IntegerField(blank=True, null=True)
class Profile(models.Model):
user_profile = models.OneToOneField(
CustomUser, on_delete=models.CASCADE, related_name="profile"
)
location = models.CharField(max_length=140, blank=True, null=True)
gender = models.CharField(max_length=140, default="Male", blank=True, null=True)
def __unicode__(self):
return u'Profile of user: %s' % self.user_profile.email
class Teacher(models.Model):
user_teacher = models.OneToOneField(
CustomUser, on_delete=models.CASCADE, related_name="teacher_account"
)
years_of_Experience = models.IntegerField(blank=True, null=True)
class Institute(models.Model):
user_institute = models.OneToOneField(
CustomUser, on_delete=models.CASCADE, related_name="Institute_account"
)
zip = models.CharField(max_length=30, null=True, blank=True)
class StudentProfile(models.Model):
user_student_profile = models.OneToOneField(
Student, on_delete=models.CASCADE, related_name="student_profile"
)
is_disabled = models.BooleanField(default=False, blank=True, null=True)
def __unicode__(self):
return u'Profile of Student: %s' % self.user_student_profile.user_student.email
class TeacherProfile(models.Model):
user_teacher_profile = models.OneToOneField(
Teacher, on_delete=models.CASCADE, related_name="teacher_profile"
)
subject = models.CharField(max_length=140, blank=True, null=True)
def __unicode__(self):
return u'Profile of teacher : %s' % self.user_teacher_profile.user_teacher.email
class InstituteProfile(models.Model):
user_institute_profile = models.OneToOneField(
Institute, on_delete=models.CASCADE, related_name="institute_profile"
)
subject = models.CharField(max_length=140, blank=True, null=True)
def __unicode__(self):
return u'Profile of Institute : %s' % self.user_institute_profile.user_institute.email
一切看起来都很好。我认为错误的原因是 INSTALLED_APPS
.
中的应用顺序
像这样更改您的 INSTALLED_APPS
并检查。
INSTALLED_APPS = [
# local apps
'users.apps.UsersConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
# third party
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'allauth.socialaccount',
'rest_auth.registration',
'corsheaders',
'crispy_forms',
]
以下是我的 settings.py 安装的应用程序部分
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
# third party
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'allauth.socialaccount',
'rest_auth.registration',
'corsheaders',
'crispy_forms',
# local apps
'users.apps.UsersConfig',
]
AUTH_USER_MODEL = 'users.CustomUser'
以下是我的apps.py文件内容
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
def ready(self):
import users.signals
以下是我的signal.py文件内容
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import Profile
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
init.py 用户应用程序中的文件为空
当我 运行 命令 python manage.py makemigrations 它给出错误 ModuleNotFoundError: No module named 'users.users' 并且它还在 apps.py 的以下行中显示错误“No module named users” 导入 users.signals
我的项目目录结构截图如附图 [在此处输入图片描述][1]
[1]: https://i.stack.imgur.com/PNU7p.png
参考上面的 django configuration/code 需要更正什么来解决 apps.py 中的错误,这样就不会解析名为 users 的模块,并为用户应用程序正确加载信号
编辑:根据其他用户提供解决方案的需要,我当前正在开发的自定义用户模型如下
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import ugettext_lazy as _
from .managers import CustomUserManager
class CustomUser(AbstractUser):
CHOICES = (
('T', 'Teacher'),
('I', 'Institute'),
('S', 'Student'),
)
role = models.CharField(max_length=1, choices=CHOICES)
email = models.EmailField(_('email address'), unique=True)
date_of_birth = models.DateField(blank=True, null=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = CustomUserManager()
def __str__(self):
return self.email
class Student(models.Model):
user_student = models.OneToOneField(
CustomUser, on_delete=models.CASCADE, related_name="student_account"
)
standard = models.IntegerField(blank=True, null=True)
class Profile(models.Model):
user_profile = models.OneToOneField(
CustomUser, on_delete=models.CASCADE, related_name="profile"
)
location = models.CharField(max_length=140, blank=True, null=True)
gender = models.CharField(max_length=140, default="Male", blank=True, null=True)
def __unicode__(self):
return u'Profile of user: %s' % self.user_profile.email
class Teacher(models.Model):
user_teacher = models.OneToOneField(
CustomUser, on_delete=models.CASCADE, related_name="teacher_account"
)
years_of_Experience = models.IntegerField(blank=True, null=True)
class Institute(models.Model):
user_institute = models.OneToOneField(
CustomUser, on_delete=models.CASCADE, related_name="Institute_account"
)
zip = models.CharField(max_length=30, null=True, blank=True)
class StudentProfile(models.Model):
user_student_profile = models.OneToOneField(
Student, on_delete=models.CASCADE, related_name="student_profile"
)
is_disabled = models.BooleanField(default=False, blank=True, null=True)
def __unicode__(self):
return u'Profile of Student: %s' % self.user_student_profile.user_student.email
class TeacherProfile(models.Model):
user_teacher_profile = models.OneToOneField(
Teacher, on_delete=models.CASCADE, related_name="teacher_profile"
)
subject = models.CharField(max_length=140, blank=True, null=True)
def __unicode__(self):
return u'Profile of teacher : %s' % self.user_teacher_profile.user_teacher.email
class InstituteProfile(models.Model):
user_institute_profile = models.OneToOneField(
Institute, on_delete=models.CASCADE, related_name="institute_profile"
)
subject = models.CharField(max_length=140, blank=True, null=True)
def __unicode__(self):
return u'Profile of Institute : %s' % self.user_institute_profile.user_institute.email
一切看起来都很好。我认为错误的原因是 INSTALLED_APPS
.
像这样更改您的 INSTALLED_APPS
并检查。
INSTALLED_APPS = [
# local apps
'users.apps.UsersConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
# third party
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'allauth.socialaccount',
'rest_auth.registration',
'corsheaders',
'crispy_forms',
]