Stack Overflow Asked by Nishan Gadtaula on February 3, 2021
i was trying to migrate and this error occurred.
django.db.utils.IntegrityError: UNIQUE constraint failed: new__bank_profile.fname_id
i was trying to extend User model and that happened.following are my views and models.
my views:
def dashboard(request):
if request.method=="POST":
fname = request.POST.get('fname')
age = request.POST.get('age')
gender = request.POST.get('gender')
address = request.POST.get('address')
occupation = request.POST.get('occupation')
bio = request.POST.get('bio')
img = request.POST.get('img')
filename = request.POST.get('filename')
user=profile(fname=fname,age=age,gender=gender,address=address,occupation=occupation,bio=bio,img=img,filename=filename)
user.save()
return render(request, 'bank/index2.html')
else:
return render(request, 'bank/signin.html')
my models:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class profile(models.Model):
fname = models.OneToOneField(User,max_length=50,default="",blank=True,null=True,on_delete=models.CASCADE,related_name='fname',unique=True)
#fname = models.CharField(max_length=500,default="",blank=True,null=True)
age = models.CharField(max_length=3,default="",blank=True,null=True)
address = models.CharField(max_length=500,default="",blank=True,null=True)
occupation = models.CharField(max_length=150, default="",blank=True,null=True)
bio = models.TextField(max_length=500, blank=True,null=True,default="")
img = models.ImageField(upload_to='bank/images', default="",blank=True,null=True)
filename = models.ImageField(upload_to='bank/images', default="",blank=True,null=True)
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default="",blank=True,null=True)
def __str__(self):
return str(self.fname)
"If you have not started with a custom user model from the very first migrate command you run, then you’re in for a world of hurt because User is tightly interwoven with the rest of Django internally. It is challenging to switch over to a custom user model mid- project. A point of confusion for many people is that custom user models were only added in Django 1.5. Up until that point the recommended approach was to add a One- ToOneField53, often called a Profile model, to User. You’ll often see this set up in older projects. But these days using a custom user model is the more common approach. However as with many things Django-related, there are implementation choices: either extend AbstractUser54 which keeps the default User fields and permissions or extend Ab- stractBaseUser55 which is even more granular, and flexible..." Extract from Django for professionals by William s. Vincent
Answered by Okwori Isaac on February 3, 2021
The fname field in the profile shouldn't allow blank values because you are extending a user model and fname field is like the primary key of the profile model, primary keys shouldn't be blank. So remove blank true and null true on the fname field.
Another problem on your view, the user shouldn't provide a primary key on the form while creating a profile. The user should come automatically from the request object. Like request.user.id. This is the best practice I guess and not:-
This is bad
fname = request. Form.get('fname')
Remove this and provide the authenticated request.user directly to the profile model when the user is creating the profile
Answered by Andrew on February 3, 2021
You defined your variables inside the "if statement". Try to change that. Try putting the "if statement" above the "user.save()".
Answered by Us_haf on February 3, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP