Django 自定义用户不能使用自定义用户的信号

Django custom user cant use signals with custom User

看看我的自定义用户和用户个人资料

class Users(AbstractUser):
    username = None
    email = models.EmailField(unique=True, null=False, db_column='email')
    is_staff = models.BooleanField(default=False, db_column='is_staff')
    is_active = models.BooleanField(default=False, db_column='is_active')
    date_joined = models.DateTimeField(default=timezone.now, db_column='date_joined')
    updated_at = models.DateTimeField(auto_now=True, auto_now_add=False, db_column='updated_at')

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name']

    objects = UsersManager()

    def __str__(self):
        return self.email

    class Meta:
        db_table = 'mn_users'
        verbose_name = 'user'
        verbose_name_plural = 'users'

class UserProfiles(models.Model):
    user_profile_id = models.AutoField(primary_key=True, db_column='user_profile_id')
    user_id = models.OneToOneField('Users', on_delete=models.CASCADE, db_column='user_id')
    photo = CloudinaryField('image')
    msisdn = models.CharField(max_length=12, null=True, blank=True, unique=True, db_column='msisdn')
    nationality_id = models.ForeignKey(Nationalities, related_name='nationalites2userprofiles', null=True, blank=True, on_delete=models.PROTECT, db_column='nationality_id')
    language_id = models.ForeignKey(Languages, related_name='preferred_languages2users', null=True, blank=True, on_delete=models.PROTECT, db_column='language_id')
    created_at = models.DateTimeField(auto_now=False, auto_now_add=True, db_column='created_at')
    updated_at = models.DateTimeField(auto_now=True, auto_now_add=False, db_column='updated_at')

    def __str__(self):
        return self.role_id

    class Meta:
        db_table = 'mn_user_profiles'
        verbose_name = 'user_profile'
        verbose_name_plural = 'user_profiles'

所以我正在使用 Django rest 框架来创建用户并且它工作正常。问题是我想要在创建用户时创建用户配置文件。为此,我制作了一个信号文件 users/signals.py

from django.db.models.signals import post_save
from django.dispatch import receiver

from .models import Users, UserProfiles


@receiver(post_save, sender=Users)
def create_profile(sender, instance, created, **kwargs):
    if created:
        UserProfiles.objects.create(users=instance)

@receiver(post_save, sender=Users)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

然后我将上面的内容添加到 users/app.py

from django.apps import AppConfig


class UsersConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'users'


class UserProfileConfig(AppConfig):
    name = 'UserProfiles'

    def ready(self):
        import users.signals

在我安装的应用程序中,我这样称呼用户应用程序

'users.apps.UsersConfig',

我已经宣布了我的

AUTH_USER_MODEL = "users.Users".

那么这里可能是什么问题。它可以很好地插入用户,但不会创建用户配置文件。我也在使用 django rest 框架的 djoser

我发现了问题您需要将信号包含在 URLs.py 文件中