Home > Blockchain >  Django Signals for User Profile (Customer and Employee)
Django Signals for User Profile (Customer and Employee)

Time:06-04

So I am trying to create a signal to create a profile for a newly created user. However, I need to have two different types of profiles: A customer profile and an employee profile. The type of profile which will be created is decided inside my User model via the "user_type" field:

user/models.py:

class User(AbstractBaseUser):
    USER_TYPES = (
        ('Employee', 'employee'),
        ('Customer', 'customer'),
        ('Vendor', 'vendor')
    )
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    phone_number = models.CharField(max_length=20)
    user_type = models.CharField(max_length=8, choices=USER_TYPES)

    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'

    def __str__(self):
        return f'{self.first_name} {self.last_name} : {self.email}'

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_admin


# if user.is_employee == True
class EmployeeProfile(models.Model):
    EMPLOYEE_ROLES = (
        ('Driver', 'driver'),
        ('Production', 'production'),
        ('Manager', 'manger')
    )
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    role = models.CharField(max_length=12, choices=EMPLOYEE_ROLES)

    def __str__(self):
        return str(self.user)


# if user.is_customer == True
class CustomerProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    company = models.CharField(max_length=100, null=True)
    address = models.CharField(max_length=100, null=True)
    address_2 = models.CharField(max_length=100, null=True)
    city = models.CharField(max_length=50, null=True)
    state = models.CharField(max_length=2, help_text="State Abbreviation (ex: OH)", null=True)
    zipcode = models.CharField(max_length=5, null=True)

    def __str__(self):
        return str(self.user)

and then in my user/signals.py file:

@receiver(post_save, sender=User)
def create_customer_profile(sender, instance, created, **kwargs):
    if created:
        if User.user_type == 'customer':
            CustomerProfile.objects.create(user=instance)


@receiver(post_save, sender=User)
def create_employee_profile(sender, instance, created, **kwargs):
    if created:
        if User.user_type == 'employee':
            EmployeeProfile.objects.create(user=instance)


@receiver(post_save, sender=User, dispatch_uid='save_new_user_profile')
def save_profile(sender, instance, created, **kwargs):
    user = instance
    if created:
        if User.user_type == 'customer':
            customer_profile = CustomerProfile(user=user)
            customer_profile.save()
    if created:
        if User.user_type == 'employee':
            employee_profile = EmployeeProfile(user=user)
            employee_profile.save()

When I create a user, no profiles are created. I was able to get a profile created by just using one Profile model and the standard signal:

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)


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

... but ideally I want to have two separate profiles depending on the type of user that is created.

Thanks in advanced.

CodePudding user response:

You can do this by overriding the save method from the User model.

def save(self, *args, **kwargs):
    super().save(*args, **kwargs)
    if self.user_type == 'customer':
        CustomerProfile.objects.create(user=self)
    elif self.user_type == 'employee':
        EmployeeProfile.objects.create(user=self)
  • Related