я учусь django и Python. У меня проблема с формой.
ошибка "TypeError at / My_app" и "не удается распаковать не повторяемый объект int"
это мои представления:
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())
мои формы:
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()
и моя модель
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')
и шаблон:
<h1>Ajout d'une NC</h1>
<form action="{% url "accueil" %}" method="GET">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
это кто-то, чтобы помочь мне понять проблему , Я искал решение в сети, но никак не мог.