首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django: IntegrityError at /accounts/accounts/myprofile唯一约束失败: accounts_userprofile.user_id

Django: IntegrityError at /accounts/accounts/myprofile唯一约束失败: accounts_userprofile.user_id
EN

Stack Overflow用户
提问于 2022-10-24 08:41:16
回答 1查看 29关注 0票数 0

当我试图更新一个现有的用户配置文件时,我会得到这个错误,

IntegrityError at /accounts/accounts/myprofile唯一约束失败: accounts_userprofile.user_id

我一直在寻找解决办法,但这对我不起作用。

模型:

类UserProfile(models.Model):

代码语言:javascript
复制
is_deligate = models.BooleanField(default=True)
is_candidate = models.BooleanField(default=False)

user = models.OneToOneField(CustomUser, 
on_delete=models.CASCADE)

name = models.CharField(max_length=200)
stud_id = models.IntegerField(primary_key = True)
course_year_and_section = models.CharField(max_length=200)

def __str__(self):
    return self.name

表格:

类UserProfileForm(ModelForm):

代码语言:javascript
复制
class Meta:
    model = UserProfile
    fields = ['name','stud_id','course_year_and_section']

意见:

def UserProf(请求):

代码语言:javascript
复制
user = request.user
ls2 = UserProfile.objects.get(user_id=user.id)
form = UserProfileForm()

if request.method == 'POST':
    form = UserProfileForm(request.POST)
    if form.is_valid():
        profile = form.save(commit=False)
        profile.user = request.user
        profile.save()
context = {'form':form,'ls2':ls2}
return render(request,"accounts/myprofile.html", context)

def UpdateUserProf(请求):

代码语言:javascript
复制
form = UserProfileForm()
try:
    prof = request.user
except UserProfile.DoesNotExist:
    prof = UserProfile(user=request.user)
if request.method == 'POST':
    form = UserProfileForm(request.POST,instance=prof)
    if form.is_valid():
        form.save()
        
        return redirect("myprofile.html")
else:
     form = UserProfileForm(instance=prof)
context = {'form':form}
return render(request,"accounts/myprofile.html", context)
EN

回答 1

Stack Overflow用户

发布于 2022-10-24 09:17:50

CustomUser只能有一个UserProfile,这就是OneToOneField的意思,也许您试图用现有的UserProfile为CustomUser设置一个UserProfile。看看这是否有帮助。

代码语言:javascript
复制
class UserProfile(models.Model):

    is_deligate = models.BooleanField(default=True)
    is_candidate = models.BooleanField(default=False)

    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, related_name="profile")

    name = models.CharField(max_length=200)
    stud_id = models.IntegerField(primary_key = True)
    course_year_and_section = models.CharField(max_length=200)

    def __str__(self):
        return self.name

def UserProf(request):
    user = request.user
    ls2 = UserProfile.objects.get(user_id=user.id)
    form = UserProfileForm()

    if request.method == 'POST':
        form = UserProfileForm(request.POST, instance=user.profile)
        if form.is_valid():
            profile = form.save()
    context = {'form':form,'ls2':ls2}
    return render(request,"accounts/myprofile.html", context)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74178545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档