使用 BaseUserAdmin 扩展我的默认用户模型后,我的信号停止工作

My signals stopped working after extending my default user model using BaseUserAdmin

我有一个工作信号,可以在创建新用户时创建一个新配置文件,但我想将用户扩展为教师、家长或管理员,然后它停止工作。

Models.py

from django.db import models
from django.contrib.auth.models import User
from PIL import Image
from django.core.validators import MaxValueValidator
from phone_field import PhoneField


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE)
    image = models.ImageField(default = 'default.jpg', upload_to = 'profile_pics')

    def __str__(self):
        return f'{self.user.username} Profile'

    def save(self):
        super().save()

        img = Image.open(self.image.path)

        if img.height > 600 or img.width > 600:
            output_size = (600,600)
            img.thumbnail(output_size)
            img.save(self.image.path)



class Parent(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    def __str__(self):
        return '{}, {}'.format(self.last_name, self.first_name)


class Teacher(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    def __str__(self):
        return '{}, {}'.format(self.last_name, self.first_name)

Admin.py

from django.contrib import admin
from .models import Profile, Student, Teacher, Parent
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User

class ParentInLine(admin.TabularInline):
    model = Parent
    can_delete = False
    verbose_name_plural = 'parent'

class TeacherInLine(admin.TabularInline):
    model = Teacher
    can_delete = False
    verbose_name_plural = 'teacher'


class UserAdmin(BaseUserAdmin):
    inlines = (ParentInLine, TeacherInLine)

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

admin.site.register(Profile)
admin.site.register(Teacher)
admin.site.register(Parent)

Signals.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()

Apps.py

from django.apps import AppConfig

class UsersConfig(AppConfig):
    name = 'users'

    def ready(self):
        import users.signals

没有错误,但我不能让它在创建新用户时自动创建配置文件。 我做错了什么吗?或者我需要做什么?

我找到了解决问题的方法。我所做的是编辑我的 "create_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 create_profile(sender, instance, created, **kwargs):
    if created:
        profile = Profile(user=instance)
        profile.save()

然后我在我的

中添加了

init.py

default_app_config = 'users.apps.UsersConfig'

然后它开始工作了。我已经被这个特殊问题困扰了 4 天,我基本上是一个有 2 到 3 周 django 经验的新手。如果您遇到同样的问题,希望这对其他人有所帮助。