简体   繁体   中英

User model in django is not extending to other models

I have created user model using AbstractBaseUser and BaseUserManager. The user model is connected with OneToOneField other three models(Employee, WorkExperience, Eduction). If I create a superuser, the user is extending to all three models. But if I create staffuser or admin user, the user is only extending to Employee model not to other three models.

models.py:

class UserProfileManager(BaseUserManager):
    """
    Creates and saves a User with the given email and password.
    """
    def create_user(self, email, username, password=None):
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
            username=username
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_staffuser(self, email,username, password):
        user = self.create_user(
            email,
            password=password,
            username=username
        )
        user.is_staff = True
        user.is_admin = True
        user.save(using=self._db)
        return user

    def create_superuser(self, email, username, password):
        user = self.create_user(
            email,
            password=password,
            username=username
        )
        user.is_staff = True
        user.is_admin = True
        user.save(using=self._db)
        return user


class UserProfile(AbstractBaseUser):
    """
    Creates a customized database table for user
    """

    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    username = models.CharField(max_length=255, unique=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_admin = models.BooleanField(default=False)

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email',]

    objects = UserProfileManager()

    def get_full_name(self):
        # The user is identified by their email address
        return self.email

    def get_short_name(self):
        # The user is identified by their email address
        return self.email

    def __str__(self):
        return 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

class Employee(models.Model):
    """
    Create employee attributes
    """

    employee_user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
    e_id = models.IntegerField(unique=True, null=True)
    first_name = models.CharField(max_length=128, null=True)
    last_name = models.CharField(max_length=128, null=True)
    .......
    @receiver(post_save, sender=UserProfile)
    def create_or_update_user_profile(sender, instance, created, **kwargs):
        if created:
            Employee.objects.create(employee_user=instance, email=instance.email)
        instance.employee.save()
    
    class WorkExperience(models.Model):
    """
    Stores employee previous work experiences
    """
    employee_user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
    employee = models.ForeignKey('Employee', related_name='we_employee', on_delete=models.CASCADE, null=True)
    previous_company_name = models.CharField(max_length=128, null=True)
    job_designation = models.CharField(max_length=128, null=True)
    from_date = models.DateField(null=True)
    to_date = models.DateField(null=True)
    job_description = models.CharField(max_length=256, null=True)

    class Education(models.Model):
    """
    Stores employee education background
    """
    employee_user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
    employee = models.ForeignKey('Employee', related_name='edu_employee', on_delete=models.CASCADE, null=True)
    institution_name = models.CharField(max_length=128, null=True)
    degree = models.CharField(max_length=128, null=True)
    passing_year = models.IntegerField(null=True)
    result = models.DecimalField(max_digits=5, decimal_places=2, null=True)

I need to extend user to other three models. How can I do that?

First of all you are not extending user model to other three table, you have created relationship with User model, and if you want to inherit it you have to write like this

class Education (UserProfile):
    pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM