how to set same unique primary key in each model of app according to user in django

i have used custom user model in my django app, first model which has been made by inheriting abstractbase user which i have rendered as a signup form, now i have created another model which i want to render after user signup in his/her profile, now i am stuck at how to assign same unique primary key in another model which i want to render after user signup, so i can access fields from both models.

from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser,PermissionsMixin



class jobglobeluserManager(BaseUserManager):
    use_in_migrations = True
    username = None
    def create_user(self, email=None, password=None, **extra_fields):
        user = self.model(email=self.normalize_email(email))
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email=None, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', True)  
        extra_fields.setdefault('is_superuser', True)  
        extra_fields.setdefault('is_active', True)
        extra_fields.setdefault('is_admin', True)


        if extra_fields.get('is_staff') is not True:  
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:  
            raise ValueError('Superuser must have is_superuser=True.')

        return self.create_user(email, password, **extra_fields)


    

class jobglobeluser(AbstractBaseUser,PermissionsMixin):
    userid = models.AutoField(primary_key=True)
    joined_on = models.DateTimeField(auto_now_add=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    useris = models.CharField(max_length=30)
    FirstName = models.CharField(max_length=20)
    LastName = models.CharField(max_length=20)
    email = models.CharField(unique=True,max_length=100)
    code = models.CharField(max_length=20)
    mobile = models.CharField(max_length=13)
    country = models.CharField(max_length=30)
    state = models.CharField(max_length=30)
    city = models.CharField(max_length=30)
    Gender = models.CharField(max_length=20)
    password = models.CharField(max_length=20)
    password2 = models.CharField(max_length=20)
    resume = models.FileField(upload_to='docs/')
    



    objects = jobglobeluserManager()
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
    
    

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True
    

class jobseekerprofile(models.Model):
    userid = models.ForeignKey(jobglobeluser, on_delete=models.CASCADE,unique=True)
    profileimg = models.ImageField(default='jobglobel/images/userpropic1.png' ,upload_to='profile_pics')
    age = models.IntegerField(default=False)

You don’t use a ForeignKey to relate the profile model to the base user - you want to use a OneToOneField

i also tried it, but unable to have same primary key according to user model

That’s fine.

You should, under no circumstances be concerned about what value the primary key is. That’s not how you refer to related objects.

then plz according to your experience tell me how to access another model ?

The same way you reference any related object.

Start by reading the referenced doc.

For example, if you have a model named User and a model named Profile, where Profile contains a OneToOneField to User, and you have an instance of User named user, then user.profile is the reference to the profile object related to user.

i tried to render it in template by calling {{user.profile}}, but it is not rendering anything in the template., ok can you just copy my code of another model first two line and tell me how to use onetoone field so i can access another model object by calling it in template.

What I provided was an example. You need to convert it to your specific models, view and template.

it is not rendering anything in template, i have used this code in second model, when i call it in template {{user.age}}, nothing showing up in template.

class jobseekerprofile(models.Model):
    user = models.OneToOneField(jobglobeluser,on_delete=models.CASCADE,primary_key=True,)
    profileimg = models.ImageField(default='jobglobel/images/userpropic1.png',upload_to='profile_pics')
    age = models.IntegerField(default=False)

What is the user object in your context that you are referencing in your template?

i have passed this as a context {‘user’: request.user}, whenever i call any of object of first model it shows me up but not for second model

Ok, so user in the context is a reference to the user object. How do you reference the profile object if you have the user object?

just changed to user object of anther model to ‘jobglobeluser’, still not working

class jobseekerprofile(models.Model):
    jobglobeluser = models.OneToOneField(jobglobeluser,on_delete=models.CASCADE,primary_key=True)
    profileimg = models.ImageField(default='jobglobel/images/userpropic1.png',upload_to='profile_pics')
    age = models.IntegerField(default=False)

Not relevant.

Please answer the question posed to you.

you mean to say, i have to pass as context for another model???

I don’t mean to say that at all.

So, if you have a reference to your user model named user, how would you refer to your profile model?

so you mean to say i have first model named ‘jobglobeluser’ and second model named ‘jobseekerprofile’ then plz explain me how can i access ‘jobseekerprofile’ objects in template?

I am trying to. But we need to work up to that point. Forget your template for the moment.

right now i am using this code , is there any mistake in this code? if it is right plz tell me what would i pass in the context for accessing second model object?


from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, PermissionsMixin



class jobglobeluserManager(BaseUserManager):
    use_in_migrations = True
    username = None

    def create_user(self, email=None, password=None, **extra_fields):
        user = self.model(email=self.normalize_email(email))
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email=None, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)
        extra_fields.setdefault('is_admin', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self.create_user(email, password, **extra_fields)


class jobglobeluser(AbstractBaseUser, PermissionsMixin):

    joined_on = models.DateTimeField(auto_now_add=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    useris = models.CharField(max_length=30)
    FirstName = models.CharField(max_length=20)
    LastName = models.CharField(max_length=20)
    email = models.CharField(unique=True, max_length=100)
    code = models.CharField(max_length=20)
    mobile = models.CharField(max_length=13)
    country = models.CharField(max_length=30)
    state = models.CharField(max_length=30)
    city = models.CharField(max_length=30)
    Gender = models.CharField(max_length=20)
    password = models.CharField(max_length=20)
    password2 = models.CharField(max_length=20)
    resume = models.FileField(upload_to='docs/')

    objects = jobglobeluserManager()
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True


class jobseekerprofile(models.Model):
    jobglobeluser = models.OneToOneField(jobglobeluser,on_delete=models.CASCADE,primary_key=True)
    profileimg = models.ImageField(default='jobglobel/images/userpropic1.png',upload_to='profile_pics')
    age = models.IntegerField(default=False)

There is nothing wrong with these models that I can see.

(Your model names don’t comply with Django and Python coding conventions, but while they would be considered confusing, there’s nothing “wrong” with doing it this way.)