1
Guys, I’m trying to make a form using model in Django that save the data when I click the button and redirect to a page, well I managed to make the form page, only after putting the necessary data and click on the save button I get the following error:
Django.db.utils.Integrityerror: NOT NULL Constraint failed: app_candidate.evaluaca_id
down my files:
py.models
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
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'),
]
py views.
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(cadastre.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>
As the evaluation field of the Candidate model is a Foreignkey tries to leave the default value as 1 or some existing value, for this there must already be a registered Criterion.
– Marlysson
evaluation = models.Foreignkey(Criterion, on_delete=models.CASCADE)... without specifying on_delete when deleting the table record will not be deleted the set of the relation that is linked. only by mouse over will have an unfilled "on_delete parameter". unfilled parameter.
– stack.cardoso
makes that small change if nothing turns out :)
– stack.cardoso