2014-11-04 119 views
6

我不断收到此错误:“NOT NULL约束失败:users_userprofile.user_id”当我尝试提交表单NOT NULL约束失败的错误

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从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}) 
+0

你能更清楚地阐述你的问题吗? – 2014-11-04 04:14:58

+1

看来你必须在使用表单时提交'user'字段。但是如果没有看到你的代码,我无法确定你的问题。 – Sadjad 2014-11-04 04:16:42

回答

4

问题是UserProfile中的用户是必需的,但您并未设置您UserProfileForm中的ser字段。数据库没有得到user_id,所以它试图在这个字段上设置null,但是字段没有null约束。您可以在UserProfile模型的字段定义中设置null = True,或覆盖保存(或可能是is_valid)窗体方法以自动设置用户,或将用户字段添加到UserProfileForm,或者任何您想要的内容。

+0

谢谢!,我的错误是在views.py中,我有form = UserProfileForm(data = request.POST)并添加了form = UserProfileForm(data = request.POST,instance = profile),它的工作原理,我认为错误是在形式或东西 – AndyJRR 2014-11-05 12:03:40

相关问题