I'm trying to make a form using Django's model that saves the data when I click the button and redirects it to a page, I managed to make the form page, only after putting the necessary data and clicking the button to save I get the following error:
django.db.utils.IntegrityError: NOT NULL constraint failed: app_candidato.avaliacao_id
below my files:
models.py
from django.db import models
from site_.settings import MEDIA_ROOT
class Criterio(models.Model):
label = models.CharField(max_length=100)
def __str__(self):
return self.label
class Candidato(models.Model):
name = models.CharField(max_length=100)
e_mail = models.EmailField(max_length=100, default = '')
github = models.URLField(default = '')
linkedin = models.URLField(max_length=100, default = '')
cover_letter = models.TextField(default = '')
Ensino_superior = models.BooleanField(default = False)
avaliacao = models.ForeignKey(Criterio, default = '')
med = models.IntegerField(default = 0)
#talvez tenha que alterrar essa linha
docfile = models.FileField(upload_to='/home/douglas/Documentos/Django/my-second-blog/site_/media', null=True, blank=True)
def __str__(self):
return self.name
urls.py
#from .models import Candidato
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.canditato_list, name='canditato_list'),
url(r'^candidato/(?P<pk>[0-9]+)/$', views.candidato_detalhe, name='candidato_detalhe'),
url(r'^cadastrar/$', views.cadastrar, name='cadastrar'),
url(r'^candidato/[0-9]+/avaliacao/$', views.avaliar, name='avaliar'),
]
views.py
from django.shortcuts import render, get_object_or_404
from .models import Candidato, Criterio
from django import forms
from .forms import CandForm, AvalForm
from django.shortcuts import redirect
def canditato_list(request):
candidatos = Candidato.objects.all()
return render(request, 'app/candidato_list.html', {'candidatos': candidatos})
def candidato_detalhe(request, pk):
candidato = get_object_or_404(Candidato, pk=pk)
return render(request, 'app/candidato_detalhe.html', {'candidato': candidato})
def avaliar(request):
criterios = Criterio.objects.all()
form2 = AvalForm()
return render(request, 'app/avaliacao.html', {'criterios': criterios})
def cadastrar(request):
if request.method == "POST":
form = CandForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.save()
return redirect('candidato_detalhe', pk=post.pk)
else:
form = CandForm()
return render(request, 'app/cadastro.html', {'form': form})
forms.py
from .models import Candidato, Criterio
from django import forms
class CandForm(forms.ModelForm):
class Meta:
model = Candidato
fields = ('name', 'e_mail', 'github', 'linkedin', 'Ensino_superior')
class AvalForm(forms.ModelForm):
class Meta:
model = Criterio
fields = ('label',)
template (cadastro.html)
<!DOCTYPE html>
<html>
<head>
<title>Cadastro</title>
</head>
<body>
<h1>Cadastro</h1>
<form method="POST" class="post-form">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Guardar</button>
</form>
</body>
</html>