gpt4 book ai didi

python - NOT NULL 约束失败错误

转载 作者:太空狗 更新时间:2023-10-30 00:06:58 24 4
gpt4 key购买 nike

当我尝试提交表单时,我不断收到此错误:“NOT NULL constraint failed: users_userprofile.user_id”

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
#Esta linea es requerira. Linkea UserProfile a un User model
user = models.OneToOneField(User)

#atributos adicionales
about_me = models.TextField(max_length=100,default='',blank=True)
experience = models.TextField(max_length=250,default='',blank=True)
offers = models.TextField(max_length=110,default='',blank=True)

这是forms.py: 从 django 导入表单 从 users.models 导入 UserProfile 从 django.contrib.auth.models 导入用户

class UserForm(forms.ModelForm):
password = forms.CharField(min_length=6,label='', widget=forms.PasswordInput(attrs={'placeholder': 'Password','required':'true','class':"form-control"}))
username = forms.CharField(label='', min_length=6,
widget=forms.TextInput(attrs={'placeholder': 'Username','required':'true','class':"form-control",'autofocus':'true'}))
email = forms.CharField(label='',
widget=forms.TextInput(attrs={'placeholder': 'Email','required':'true','class':"form-control"}))

class Meta:
model = User
fields = ('username', 'email', 'password')

class UserProfileForm(forms.ModelForm):
about_me = forms.CharField(label='',
widget=forms.Textarea(attrs={'placeholder': 'Sobre mi','required':'true','class':"form-control"}))
first_name = forms.CharField(label='',
widget=forms.TextInput(attrs={'placeholder': 'Nombre','required':'true','class':"form-control"}))
last_name = forms.CharField(label='',
widget=forms.TextInput(attrs={'placeholder': 'Apellidos','required':'true','class':"form-control"}))
experience = forms.CharField(label='',
widget=forms.TextInput(attrs={'placeholder': 'Experiencia','required':'true','class':"form-control"}))
offers = forms.CharField(label='',
widget=forms.Textarea(attrs={'placeholder': 'Mensaje','required':'true','class':"form-control"}))

class Meta:
model = UserProfile
fields =('first_name','last_name','about_me','experience','offers')

这是模板:

{%extends 'base.html'%}

{%block content%}
{% if user.is_authenticated %}
<h1>Edita tu perfil</h1>
<form id='profile' method='post' action='/edit_profile/'>
{% csrf_token %}

{{profile.as_p}}

<button type='submit'>Editar</button>
</form>
{%endif%}

{%endblock%}

先谢谢

编辑:

错误在 views.py 中,我需要向表单添加一个实例,如下所示:

form = UserProfileForm(data=request.POST, instance=profile)

这是我完整的views.py:

def edit_profile(request):
try:
profile = request.user.userprofile

except UserProfile.DoesNotExist:

profile = UserProfile(user=request.user)

if request.method == 'POST':
form = UserProfileForm(data=request.POST, instance=profile)

if form.is_valid():

form.save()

return HttpResponse("Exito!")
else:
form = UserProfileForm(instance=profile)

else:
form = UserProfileForm()

return render(request,
'editprof.html',
{ 'form': form})

最佳答案

问题是 UserProfile 中的 user 是必需的,但您没有在 UserProfileForm 中设置 user 字段。数据库未获得 user_id,因此它尝试在此字段上设置 null,但该字段没有 null 约束。您可以在 UserProfile 模型中的字段定义上设置 null=True,或者覆盖 save(或者可能是 is_valid)表单自动设置用户或将用户字段添加到 UserProfileForm 或任何您想要的方法。

关于python - NOT NULL 约束失败错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26727616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com