Python Django:无法解压缩不可编辑的int对象
我正在学习django和Python。我对表格有问题 错误为“TypeError at/My_app”和“无法解压缩不可编辑的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
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})