The problem arises while creating new users. It says related object does not exist.
accounts model
class Accounts(AbstractBaseUser):
username = models.CharField(max_length=30, unique=True)
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
date_created = models.DateTimeField(
verbose_name='date_joined', auto_now_add=True)
last_login = models.DateTimeField(
verbose_name='last_login', auto_now_add=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
# setting username as a key to login. Note: email can also be used in it
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['first_name', 'last_name', 'email']
store model
class Customer(models.Model):
username = models.OneToOneField(
Accounts, on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length=100, null=True)
email = models.CharField(max_length=100, null=True)
def __str__(self):
return self.name
in your views as the error says you tried
customer = request.user.customer
while you have nothing namer customer a all in your Customer class but a username
so either :
change your query in the views to :
customer = request.user.username
or
change the username in your class to customer :
customer = models.OneToOneField( Accounts, on_delete=models.CASCADE, null=True,
blank=True)
Related
Here i have two models Keys and Users i am creating POST API so i got encounter with this Error .
UserModel.py:
class Users(AbstractBaseUser):
vendor_name = models.ForeignKey(Vendor, on_delete=models.CASCADE, default=None)
key_value = models.ForeignKey(KeyTable, on_delete=models.CASCADE, default=None, null=True)
username = models.CharField(max_length=100, verbose_name="username", unique=True)
password = models.CharField(max_length=100)
hardware_id = models.CharField(max_length=150, null=True)
created_by = models.DateField(verbose_name="created_by", auto_now_add=True)
USERNAME_FIELD = "username"
REQUIRED_FIELDS = ['password', 'hardware_id']
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_role_vendor = models.BooleanField(default=False)
is_role_customer = models.BooleanField(default=True)
def __str__(self):
return self.username
objects = UserManager()
KeyModel.py:
class KeyTable(models.Model):
key_id = models.IntegerField(unique=True, auto_created=True)
key_value = models.CharField(max_length=100)
issue_date = models.DateField(max_length=100)
expiry_date = models.DateField()
status = models.CharField(max_length=50)
license_tenure = models.IntegerField()
def __str__(self):
return self.key_value
KeySerializer:
class KeySerializer(serializers.ModelSerializer):
class Meta:
model: Keys
fields = ['key_id', 'key_value', 'issue_date', 'expiry_date', 'status', 'license_tenure']
Error Encounters:
django.db.utils.IntegrityError: NOT NULL constraint failed: app_users.key_value_id
you can set null=True and blank=True in the model field which is causing you an error
or you can delete the app and create it again with the same code this trick can also work.
But if you do not find solution then you can find several solutions here.
What am I doing wrong?
Django Models:
USER MODEL
class User(AbstractUser):
# DEFINE MODEL FIELDS
related_person = models.OneToOneField(Person, on_delete=models.CASCADE, default='')
user_id = models.BigAutoField(verbose_name='User ID', primary_key=True, serialize=False, auto_created=True)
email = models.EmailField(verbose_name='Email', max_length=80, blank=False, unique=True, null=False, default='')
is_verified = models.BooleanField(verbose_name='Email Verified', blank=False, default=False)
date_joined = models.DateField(verbose_name='date joined', auto_now_add=True, null=True)
last_login = models.DateField(verbose_name='last login', auto_now=True, null=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
first_name = None
last_name = None
username = None
# IMPORT MODEL MANAGER CLASS
objects = UserManager()
# SET LOGIN FIELD
USERNAME_FIELD = 'email'
# SET REQUIRED FIELDS
REQUIRED_FIELDS = ['is_verified']
PERSON MODEL
class Person(models.Model):
class Titles(models.TextChoices):
MR = 'Mr', _('Mr')
MRS = 'Mrs', _('Mrs')
MISS = 'Miss', _('Miss')
MS = 'Ms', _('Ms')
DR = 'Dr', _('Dr')
related_entity = models.OneToOneField(Entity, on_delete=models.CASCADE)
person_id = models.BigAutoField(verbose_name='Person ID', primary_key=True, serialize=False, auto_created=True)
first_name = models.CharField(verbose_name='First Name', max_length=50, blank=False, null=False)
last_name = models.CharField(verbose_name='Last Name', max_length=50, blank=False, null=False)
title = models.CharField(verbose_name='Title', max_length=4, choices=Titles.choices, blank=True, null=False, default='')
alias = models.CharField(verbose_name='Alias', max_length=150, blank=True, null=False)
ENTITY MODEL
class Entity(models.Model):
class EntityTypes(models.TextChoices):
PERSON = 'Person', _('Person')
COMPANY = 'Company', _('Company')
entity_type = models.CharField(
verbose_name='Legal Entity Type',
max_length=7,
choices=EntityTypes.choices,
default=EntityTypes.PERSON,
blank=False,
null=False)
I have a Sign Up form that saves a person's email to the User Model, and password to the User Model, after checking the email doesn't already exist, and passwords (when entered twice), match. but then I get this error when I save the form:
null value in column "related_person_id" of relation "customauth_user" violates not-null constraint
DETAIL: Failing row contains (1, pbkdf2_sha256$320000$Ipsu5kX2Opd2a1j9996ReS$uPR4GCbSxS7+9+0KIAaE..., sachin#cantab.net, f, 2022-02-28, 2022-02-28, t, f, f, f, null).
Any ideas?
The overall idea behind the models was to allow a someone to signup with an email and password, through the User model. Then once they've logged in, they can enter their firstname, lastname, title, alias, etc, in the Person model.
Your User-Model does not allow null to be set as value for the column related_person (Django's ORM transpiles this to related_person_id in SQL)
By default this causes SQL to put a Null-Constraint on that column.
However you can prevent Django from implementing such a Constraint, by simply parsing null = True as additional argument to the affected column of your Model.
Thus you can simply change your Models to:
class User(AbstractUser):
# DEFINE MODEL FIELDS
related_person = models.OneToOneField(Person, on_delete=models.CASCADE, default='', null=True) # allowing null-values
user_id = models.BigAutoField(verbose_name='User ID', primary_key=True, serialize=False, auto_created=True)
email = models.EmailField(verbose_name='Email', max_length=80, blank=False, unique=True, null=False, default='')
is_verified = models.BooleanField(verbose_name='Email Verified', blank=False, default=False)
date_joined = models.DateField(verbose_name='date joined', auto_now_add=True, null=True)
last_login = models.DateField(verbose_name='last login', auto_now=True, null=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
first_name = None
last_name = None
username = None
# IMPORT MODEL MANAGER CLASS
objects = UserManager()
# SET LOGIN FIELD
USERNAME_FIELD = 'email'
# SET REQUIRED FIELDS
REQUIRED_FIELDS = ['is_verified']
# ...
I am using a custom user model and I have anther Enquiry model. I want that is the login user type is_service_provider I want to fetch data of service provider who had login in the panel. I don't know how to do this.
here is my models.py
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField(max_length=254,unique=True)
name = models.CharField(max_length=254, null=True)
email = models.EmailField(max_length=254, null=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_Customer = models.BooleanField(default=False)
is_Service_Provider = models.BooleanField(default=False)
last_login = models.DateTimeField(null=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'username'
EMAIL_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
def get_absolute_url(self):
return "/users/%i/" % (self.pk)
def get_username(self):
return self.username
class Enquiry(models.Model):
name = models.CharField(max_length=254 , null=False)
email = models.EmailField(max_length=254, null=False)
phone_no = models.IntegerField(null=False)
service_requied = models.ForeignKey('accounts.Category', on_delete=models.CASCADE, null=False)
subcategory = models.ForeignKey('accounts.SubCategory', on_delete=models.CASCADE, null=False)
country = models.ForeignKey('accounts.Country', on_delete=models.CASCADE, null=False)
state =models.ForeignKey('accounts.State', on_delete=models.CASCADE, null=False)
city = models.ForeignKey('accounts.City', on_delete=models.CASCADE, null=False)
address = models.CharField(max_length=254, null=False)
defects = models.TextField(max_length=254, null=False)
service = models.ForeignKey('accounts.User', on_delete=models.CASCADE, null=False)
def __str__(self):
return self.service.username
Here is my views.py
#login_required
def enquiry(request):
user = Enquiry.objects.all()
data = request.user
return render(request, 'service-provider-panel/enquiry.html', {'user' : user, 'data':data})
I have a user model which looks like this:
class User(AbstractUser):
first_name = models.CharField(max_length=70)
last_name = models.CharField(max_length=70)
middle_name = models.CharField(max_length=70, blank=True)
email = models.EmailField(
max_length=254,
unique=True,
verbose_name='Email Address',
blank=True
)
is_student = models.BooleanField(default=False, verbose_name='Student')
is_superuser = models.BooleanField(default=False, verbose_name='Administrator')
is_teacher = models.BooleanField(default=False, verbose_name='Teacher')
is_staff = models.BooleanField(default=False, verbose_name='Staff')
is_registrar = models.BooleanField(default=False, verbose_name='Registrar')
and in my StudentPorile model I have a field called class_advisor which is related to User model and I need to get the users with is_teacher field is True. Now, how do i filter class_advisor field to return Users only with is_teacher=True?
here's my StudentProfile model:
class StudentProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
lrn = models.CharField(max_length=20)
landline_number = models.CharField(max_length=11, null=True, blank=True)
mobile_number = models.CharField(max_length=11, null=True, blank=True)
address = models.TextField(max_length=300, blank=True)
mothers_name = models.CharField(max_length=50)
fathers_name = models.CharField(max_length=50)
contact_person_name = models.CharField(max_length=50)
contact_person_number = models.CharField(
max_length=12, verbose_name='Phone number of Contact Person')
# class_advisor = IM STUCK HERE
year_level = models.OneToOneField(Year_Level, on_delete=models.SET_NULL, null=True)
section = models.OneToOneField(Section, on_delete=models.SET_NULL, null=True)
This should return you a queryset of all the Users who have their is_teacher attribute set to True
User.objects.filter(is_teacher=True)
views.py
class MyView(View):
def get(self, request):
filtered_users = User.objects.filter(is_teacher=True)
return render(request, 'home.html', context={'users': filtered_users})
template.html
{% for user in users %}
{{user}}
{% endfor %}
I created Custom User model in django using below code
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
username = models.CharField(_("Username"), max_length=50, unique=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(default=timezone.now)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', ]
objects = CustomUserManager()
def __str__(self):
return self.email
And I also created another model for user profiles like below.
class UserProfile(models.Model):
user = models.OneToOneField(User, verbose_name=_("User"), on_delete=models.CASCADE)
first_name = models.CharField(_("First name"), max_length=50)
middle_name = models.CharField(_("Middle name"), max_length=50, blank=True, null=True)
last_name = models.CharField(_("Last name"), max_length=50)
dob = models.DateField(_("D.O.B"), auto_now=False, auto_now_add=False, blank=False, null=False)
profile_image = models.ImageField(_("Profile picture"), upload_to='user/profile/', blank=True)
webiste_link = models.URLField(_("Websites"), max_length=200, null=True, blank=True)
At last I created another model for Category like below.
class Category(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_(
"Author"), on_delete=models.CASCADE, related_name='blog_category')
title = models.CharField(_("Title"), max_length=50)
I want to auto save logged in user full name instead of user email address. By the way i used below code to auto save logged in user. It works but It only save user email address. But I want user full name instead of email or username.
obj.author = request.user
super().save_model(request, obj, form, change)
You could define a method in the UserProfile model to build this fullname, as you may need it in another function :
class UserProfile(models.Model):
user = models.OneToOneField(User, verbose_name=_("User"), on_delete=models.CASCADE)
[...]
def get_fullname(self):
return ' '.join([name for name in [
self.first_name,
self.middle_name,
self.last_name
] if name])
So you keep this way to save the user foreignkey in a Category:
obj.author = request.user
And later, to call the fullname on a category instance you can use :
# cat = Category.objects.first()
cat.author.userprofile.get_fullname()
You can add a filed called full_name in UserProfile model
Class UserProfile(models.Model):
user = models.OneToOneField(User, verbose_name=_("User"), on_delete=models.CASCADE)
first_name = models.CharField(_("First name"), max_length=50)
full_name = mdoels.CharField(_("Full name"),max_length=100)
middle_name = models.CharField(_("Middle name"), max_length=50, blank=True, null=True)
last_name = models.CharField(_("Last name"), max_length=50)
dob = models.DateField(_("D.O.B"), auto_now=False, auto_now_add=False, blank=False, null=False)
profile_image = models.ImageField(_("Profile picture"), upload_to='user/profile/', blank=True)
webiste_link = models.URLField(_("Websites"), max_length=200, null=True, blank=True)
def save(self,*args,**kwargs):
if not self.full_name:
self.full_name = self.first_name + " " + self.middle_name + " " + self.last_name
super().save(*args, **kwargs)
And then using author you can call the users full_name