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)
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.
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)
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?
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.)