【问题标题】:Django : cannot unpack non-iterable int objectDjango:无法解压不可迭代的 int 对象
【发布时间】:2020-07-21 05:15:47
【问题描述】:

我正在学习 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>

是否有人帮助我理解问题。我在网上搜索了解决方案,但没有办法。

【问题讨论】:

  • 为什么不使用ModelForm 这样可以删除大部分样板代码。

标签: python django django-models django-forms django-templates


【解决方案1】:

问题是这样的查询:

ncs = NC.objects.values_list('idaffaire', flat=True)

将导致ncs 成为int 对象的互斥体。但是 ChoiceFieldchoices 需要一个 2-tuples 列表,其中 key 作为这些 2-tuples 的第一项,标签作为 2-tuples 的第二项。 p>

不过,在类级别使用查询不是一个好主意。这意味着查询将在加载类时执行。这意味着,如果您稍后添加额外的 NC 对象,表单将不会提供此作为新选择。

我还建议改用ModelForm [Django-doc],因为这将删除大量样板代码,尤其是因为idposte 需要为Poste 对象使用有效值,而在这里您的表单无法验证。

然后您可以将表单实现为:

from django import forms
from .models import NC

class NCForm(forms.ModelForm):
    class Meta:
        model = NC
        fields = '__all__'

然后在您的views.py 中,您可以将此表单用于:

from django.shortcuts import redirect

def view_accueil(request):
    if request.method == 'POST':
        form = NCForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('name-of-a-view')
    else:
        form = NCForm()
    return render(request, 'Qualite/accueil.html', {'form': form})

注意:如果 POST 请求成功,请发送redirect [Django-doc] 实施Post/Redirect/Get pattern [wiki]。 这样可以避免在用户刷新时发出相同的 POST 请求 浏览器。

【讨论】:

  • @ROGGYQUENTIN:您需要指定视图的名称,而不是 'name-of-view'。这是要重定向到的视图的名称。因此,例如,如果您有url('some/url', some_view, name='name_of_my_view'),则可以redirect('name_of_my_view')
猜你喜欢
  • 2019-04-22
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
  • 2021-06-17
  • 2021-06-06
  • 2021-10-03
  • 2019-04-14
  • 2020-01-28
相关资源
最近更新 更多