简体   繁体   English

django.db.utils.IntegrityError:唯一约束失败:new__bank_profile.fname_id

[英]django.db.utils.IntegrityError: UNIQUE constraint failed: new__bank_profile.fname_id

i was trying to migrate and this error occurred.我正在尝试迁移,但发生了此错误。

django.db.utils.IntegrityError: UNIQUE constraint failed: new__bank_profile.fname_id django.db.utils.IntegrityError:唯一约束失败: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)

You defined your variables inside the "if statement".您在“if 语句”中定义了变量。 Try to change that.尝试改变这一点。 Try putting the "if statement" above the "user.save()".尝试将“if 语句”放在“user.save()”之上。

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 .因为你是扩展用户模式在配置文件中的FNAME不应该空值FNAME场就像剖面模型的主键主键不应该是空白 So remove blank true and null true on the fname field.因此,删除fname字段上的空白 truenull tr​​ue

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 .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删除它并在用户创建配置文件时将经过身份验证的request.user直接提供给配置文件模型

"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. “如果您从运行的第一个 migrate 命令开始就没有开始使用自定义用户模型,那么您将陷入困境,因为 User 在内部与 Django 的其余部分紧密交织在一起。切换到一个具有挑战性的自定义用户模型中期项目。许多人混淆的一点是自定义用户模型仅在 Django 1.5 中添加。在那之前,推荐的方法是向 User 添加 One-ToOneField53,通常称为 Profile 模型。你会经常在较旧的项目中看到这种设置。但现在使用自定义用户模型是更常见的方法。但是,与 Django 相关的许多事情一样,有实现选择:要么扩展 AbstractUser54 保留默认用户字段,要么权限或扩展 Ab-stractBaseUser55,它更精细、更灵活......” 摘自 Django for Professionals by William s. Vincent文森特

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django.db.utils.IntegrityError:唯一约束失败:core_profile.user_id - django.db.utils.IntegrityError: UNIQUE constraint failed: core_profile.user_id django.db.utils.IntegrityError:UNIQUE约束失败:rango_page__new.category_id - django.db.utils.IntegrityError: UNIQUE constraint failed: rango_page__new.category_id django.db.utils.IntegrityError:唯一约束失败:new__vacancies_company.owner_id 错误 - django.db.utils.IntegrityError: UNIQUE constraint failed: new__vacancies_company.owner_id error django.db.utils.IntegrityError:NOT NULL 约束失败:users_profile.user_id - django.db.utils.IntegrityError: NOT NULL constraint failed: users_profile.user_id Django 表单错误:django.db.utils.IntegrityError:唯一约束失败:legal_useragreedtolegal.user_id - Django form error: django.db.utils.IntegrityError: UNIQUE constraint failed: legal_useragreedtolegal.user_id Django抛出错误django.db.utils.IntegrityError:唯一约束失败:mediaSort_userdata.user_id - Django throwing error django.db.utils.IntegrityError: UNIQUE constraint failed: mediaSort_userdata.user_id django.db.utils.IntegrityError:唯一约束失败:beachprofile_beach.beach_id - django.db.utils.IntegrityError: UNIQUE constraint failed: beachprofile_beach.beach_id django.db.utils.IntegrityError:唯一约束失败:new__main_doctor.email - django.db.utils.IntegrityError: UNIQUE constraint failed: new__main_doctor.email django.db.utils.IntegrityError:唯一约束失败:auctions_bids.item_id - django.db.utils.IntegrityError: UNIQUE constraint failed: auctions_bids.item_id django.db.utils.IntegrityError:唯一约束失败:mode_setting.user_id - django.db.utils.IntegrityError: UNIQUE constraint failed: mode_setting.user_id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM