我正在一个模板中显示一个表单和表单集,并尝试保存它们。 Formset可以保存很好,但是当我添加表单时,它会给出'NoneType'对象不可迭代错误。
Django显示我在此阶段出错,而Chrome显示我的变量,如下所示。
问题行:po = Purchaseorder_form.save()
▼本地变量
ProductBatchFormset <“类'django.forms.formsets.ProductBatchFormFormSet'>
productbatch_formset <“ django.forms.formsets.ProductBatchFormFormSet对象位于0x10bc39950”>
purchaseorder_form <“ PurchaseOrderForm bound =真,有效=真,字段=(purchaseOrderId; issuedBy; issuedAt; issuedTo; referencePurchaseOrders; internalManager; note; productSpendType)”>
request <“ WSGIRequest:POST'/购买订单/创建?submitted = True'”>
提交的错误
完整追溯 追溯(最近一次通话): 文件“ /Users/marinalee/.local/share/virtualenvs/scm-SrDaDYy6/lib/python3.7/site-packages/django/core/handlers/exception.py”,第34行,位于内部 响应= get_response(请求) 文件“ /Users/marinalee/.local/share/virtualenvs/scm-SrDaDYy6/lib/python3.7/site-packages/django/core/handlers/base.py”,行115,位于_get_response 响应= self.process_exception_by_middleware(e,请求) 文件“ /Users/marinalee/.local/share/virtualenvs/scm-SrDaDYy6/lib/python3.7/site-packages/django/core/handlers/base.py”,行_get_response中的113行 响应= wraped_callback(请求,* callback_args,** callback_kwargs) 在create_purchaseOrder中,文件“ /Users/marinalee/django-projects/scm/purchaseorders/views.py”,第22行 po = Purchaseorder_form.save() 保存中的文件“ /Users/marinalee/.local/share/virtualenvs/scm-SrDaDYy6/lib/python3.7/site-packages/django/forms/models.py” self._save_m2m() 文件“ /Users/marinalee/.local/share/virtualenvs/scm-SrDaDYy6/lib/python3.7/site-packages/django/forms/models.py”,行441,在_save_m2m中 f.save_form_data(self.instance,cleaned_data [f.name]) 第1621行的文件“ /Users/marinalee/.local/share/virtualenvs/scm-SrDaDYy6/lib/python3.7/site-packages/django/db/models/fields/related.py”在save_form_data中 getattr(instance,self.attname).set(data) 设置中的文件“ /Users/marinalee/.local/share/virtualenvs/scm-SrDaDYy6/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py” objs =元组(objs) TypeError:“ NoneType”对象不可迭代
# forms.py
class PurchaseOrderForm(forms.ModelForm):
class Meta:
model = PurchaseOrder
fields = '__all__'
issuedAt = forms.DateField(
widget=forms.DateInput(format='%Y%m%d', attrs={'placeholder': 'YYYYMMDD'}),
input_formats=('%Y%m%d', )
)
referencePurchaseOrders = forms.ModelChoiceField(widget=forms.SelectMultiple, queryset=PurchaseOrder.objects.all(), required=False)
internalManager = forms.ModelChoiceField(queryset=CustomUser.objects.all(), required=False)
issuedBy = forms.ModelChoiceField(queryset=Company.objects.all(), required=False)
issuedTo = forms.ModelChoiceField(queryset=Company.objects.all(), required=False)
productSpendType = forms.ModelChoiceField(queryset=ProductSpendType.objects.all(), required=False)
class ProductBatchForm(forms.ModelForm):
class Meta:
model = ProductBatch
fields = '__all__'
product = forms.ModelChoiceField(queryset=Product.objects.all(), required=False)
quantity = forms.CharField(required=False)
unitprice = forms.CharField(required=False)
unitcurrency = forms.ModelChoiceField(queryset=Currency.objects.all(), required=False)
productSpendType = forms.ModelChoiceField(queryset=ProductSpendType.objects.all(), required=False)
# models.py
class PurchaseOrder(models.Model):
def __str__(self):
return str(self.pk)
purchaseOrderId = models.CharField(max_length=30, null=True, blank=True, db_index=True)
issuedBy = models.ForeignKey('users.Company', null=True, blank=True, db_index=True, on_delete=models.CASCADE, related_name='issued_POs')
issuedAt = models.DateTimeField(null=True, blank=True, db_index=True)
issuedTo = models.ForeignKey('users.Company', null=True, blank=True, db_index=True, on_delete=models.CASCADE, related_name='received_POs')
referencePurchaseOrders = models.ManyToManyField('self', symmetrical=False, null=True, blank=True, db_index=True, related_name='related_POs')
internalManager = models.ForeignKey('users.CustomUser', null=True, blank=True, db_index=True, on_delete=models.CASCADE, related_name='managed_POs')
note = models.TextField(max_length=500, null=True, blank=True, db_index=True)
class ProductBatch(models.Model):
def __str__(self):
return str(self.pk)
purchaseOrder = models.ForeignKey('PurchaseOrder', null=True, blank=True, db_index=True, on_delete=models.CASCADE)
product = models.ForeignKey('products.Product', null=True, blank=True, db_index=True, on_delete=models.CASCADE)
quantity = models.IntegerField(null=True, blank=True, db_index=True)
unitprice = models.DecimalField(max_digits=20, decimal_places=2, null=True, blank=True, db_index=True)
unitcurrency = models.ForeignKey('payments.Currency', null=True, blank=True, db_index=True, on_delete=models.CASCADE)
productSpendType = models.ForeignKey('ProductSpendType', on_delete=models.CASCADE, null=True, blank=True, db_index=True)
#views.py
def create_purchaseOrder(request):
submitted = False
ProductBatchFormset = formset_factory(ProductBatchForm, extra=1, can_order=True, can_delete=True)
if request.method == 'POST':
purchaseorder_form = PurchaseOrderForm(request.POST)
productbatch_formset = ProductBatchFormset(request.POST, prefix = 'pb')
if purchaseorder_form.is_valid() and productbatch_formset.is_valid():
po = purchaseorder_form.save()
for form in productbatch_formset:
pb = form.save()
return HttpResponseRedirect('/purchaseorder/create?submitted=True')
else:
return HttpResponseRedirect('/purchaseorder/create?'+str(purchaseorder_form.errors))
else:
context = {
'purchaseorder_form': PurchaseOrderForm(),
'productbatch_formset': ProductBatchFormset(prefix='pb')
}
return render(request, 'createpo.html', context)
#template
<form action="" method="post" id="myForm">
{% csrf_token %}
<div class="form-row" >
<div class="form-group col-md-3">
<label for="issuedBy" class="control-label">Issued by</label>
{{purchaseorder_form.issuedBy}}
</div>
<div class="form-group col-md-3">
<label for="issuedTo" class="control-label">Issued to</label>
{{purchaseorder_form.issuedTo}}
</div>
</div>
<div class="form-row">
<div class="form-group col-md-2">
<label for="purchaseOrderId" class="control-label">PO number</label>
{{purchaseorder_form.purchaseOrderId}}
</div>
<div class="form-group col-md-2">
<label for="issuedAt" class="control-label">PO date</label>
{{purchaseorder_form.issuedAt}}
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="internalManager" class="control-label">Manager</label>
{{purchaseorder_form.internalManager}}
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="referencePurchaseOrders" class="control-label">Reference POs</label>
{{purchaseorder_form.referencePurchaseOrders}}
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="note" class="control-label">Notes</label>
{{purchaseorder_form.note}}
</div>
</div>
<br/>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
{% for productbatch_form in productbatch_formset.forms %}
<tr>
<td>Product: {{productbatch_form.product}}</td>
<td>Quantity: {{productbatch_form.quantity}}</td>
<td>Currency: {{productbatch_form.unitcurrency}}</td>
<td>Unit Price: {{productbatch_form.unitprice}}</td>
<td>Spend Type: {{productbatch_form.productSpendType}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ productbatch_formset.management_form }}
<div>
<br/><br/>
<button class="btn btn-primary" type="submit">Create PO</button></div>
{% csrf_token %}
</form>
我希望表单以及表单集中的表单都可以保存,但是只有表单集中的表单可以保存。非常感谢您的帮助!
答案 0 :(得分:0)
问题似乎是,#include <Servo.h>
Servo throttle;
Servo steering;
String input;
int t;
int s;
void setup()
{
throttle.attach(3);
steering.attach(5);
throttle.write(0);
steering.write(125);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
input = Serial.readString();
char mode = input.charAt(0);
if(mode == 't')
{
t = input.substring(1).toInt();
}
else if(mode=='s')
{
s = input.substring(1).toInt();
}
throttle.write(t);
steering.write(s);
Serial.println(input);
delay (10);
}
}
字段的SelectMultiple
小部件在未选择参考采购订单且内部多对多代码期望可迭代的情况下返回referencePurchaseOrders
。我尚未对此进行测试,但我相信以下方法会起作用:
None