2014-11-04 161 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

相關問題