djangodjango-signalsdjoser

Django custom user cant use signals with custom User


Have a look at my custom User and user profile

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'

So I am using Django rest framework to create the user and this is working properly. Issue is I want a user Profile created when a user is created. For this i made a signals file in 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()

I then added the above to the 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

On my installed apps I call the users app as such

'users.apps.UsersConfig',

and I have declared my

AUTH_USER_MODEL = "users.Users".

So what could be the issue here. It inserts the user just fine but doesn't create the user profile. I'm also using djoser with django rest framework


Solution

  • In the urls.py:

    from .signals import *