Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:无法解压缩不可编辑的int对象_Python_Django_Django Models_Django Forms_Django Templates - Fatal编程技术网

Python Django:无法解压缩不可编辑的int对象

Python Django:无法解压缩不可编辑的int对象,python,django,django-models,django-forms,django-templates,Python,Django,Django Models,Django Forms,Django Templates,我正在学习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 NCF

我正在学习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>
Ajout d'une NC
{%csrf_令牌%}
{{form}}

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

问题在于,类似以下的查询:

ncs = NC.objects.values_list('idaffaire', flat=True)
将导致
ncs
成为
int
对象的一个交互变量。但是
选择字段
选项
需要一个2元组列表,其中键是这2元组的第一项,标签是这2元组的第二项

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

我还建议改为使用a,因为这将删除大量样板代码,特别是因为例如
idposte
需要为
Poste
对象使用有效值,而您的表单无法验证这一点

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

from django import forms
from .models import NC

class NCForm(forms.ModelForm):
    class Meta:
        model = NC
        fields = '__all__'
注意:如果POST请求成功,您应该 实施。 这样可以避免在用户刷新日志时发出相同的POST请求 浏览器


为什么不使用
ModelForm
,这样大部分样板代码都可以删除。@ROGGYQUENTIN:您需要指定视图的名称,而不是
'name-of-view'
。这是要重定向到的视图的名称。因此,如果您有一个
url('some/url',some\u view,name='name\u of theu my\u view')
,那么您可以
重定向('name\u of theu my\u view')
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})