i'm learning django and Python. I have a problem with a form.
the error is "TypeError at /My_app" and "cannot unpack non-iterable int object"
this is my views :
from django.http import HttpResponse, Http404
from django.shortcuts import redirect, render, get_object_or_404
from datetime import datetime
from Qualite.forms import NCForm
from Qualite.models import NC, Nc_type, Poste
def view_accueil(request):
form = NCForm(request.POST or None)
if form.is_valid():
newNc = NC()
newNc.idaffaire = form.cleaned_data['idaffaire']
newNc.idnc = form.cleaned_data['idnc']
newNc.idof = form.cleaned_data['idof']
newNc.idposte = form.cleaned_data['idposte']
newNc.idrepere = form.cleaned_data['idrepere']
newNc.iquantite = form.cleaned_data['iquantite']
newNc.save()
return render(request, 'Qualite/accueil.html', locals())
my forms :
from django import forms
from .models import Nc_type, NC, Poste
class NCForm(forms.Form):
#choices = NC.objects.values_list('id', 'idaffaire')
ncs = NC.objects.values_list('idaffaire', flat = True)
idaffaire = forms.ChoiceField(choices = (ncs))
idof = forms.CharField()
idrepere = forms.CharField()
idposte = forms.CharField()
idnc = forms.CharField()
quantite = forms.CharField()
and my model
from django.db import models
from django.utils import timezone
class Nc_type(models.Model):
nom = models.CharField(max_length=30)
def __str__(self):
return self.nom
class Poste(models.Model):
nom = models.CharField(max_length=50)
def __str__(self):
return self.nom
class NC(models.Model):
idaffaire = models.CharField(max_length=4, db_column='idAffaire')
idof = models.IntegerField(db_column='idOf')
idposte = models.ForeignKey('Poste', models.DO_NOTHING, db_column="idPoste", default=1)
idrepere = models.IntegerField(db_column='idRepere')
idnc = models.ForeignKey(Nc_type, models.DO_NOTHING, db_column='idNc_type', default=1)
quantite = models.PositiveIntegerField()
dateajout = models.DateTimeField(default=timezone.now, db_column='dateAjout')
and the template:
<h1>Ajout d'une NC</h1>
<form action="{% url "accueil" %}" method="GET">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
is it someone to help me to understand the problem. I searched solution on the web but no way.
ModelForm
such that most of the boilerplate code can be removed. – Willem Van Onsem