Monday, 15 September 2014

python - NOT NULL constraint failed error -



python - NOT NULL constraint failed error -

i maintain getting error: "not null constraint failed: users_userprofile.user_id" when seek submit form

from django.db import models django.contrib.auth.models import user class userprofile(models.model): #esta linea es requerira. linkea userprofile united nations 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)

this forms.py: django import forms users.models import userprofile django.contrib.auth.models import user

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')

this template:

{%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%}

thanks before hand

edit:

the error in views.py needed add together instance form, this:

form = userprofileform(data=request.post, instance=profile)

this finish 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() homecoming httpresponse("exito!") else: form = userprofileform(instance=profile) else: form = userprofileform() homecoming render(request, 'editprof.html', { 'form': form})

problem user in userprofile required, not setting user field in userprofileform. database didn't user_id, tried set null on field, field have not not null constraint. can set null=true on field definition in userprofile model, or overwrite save (or is_valid) form method set user automatically, or add together user field userprofileform, or whatever want.

python django python-2.7

No comments:

Post a Comment