在表单集验证时,ManagementForm数据丢失错误
当我尝试使用中间m2m表子formset保存父表单时出现此错误。我不知道如何解决,因为在Traceback中缺少有关此错误的信息。请帮忙!
models.py
class Material(models.Model):
name = models.CharField(max_length=200)
familiy = models.ForeignKey(Material_family, on_delete=models.CASCADE, null=True)
…
class Purchase(models.Model):
number = models.IntegerField()
date = models.DateField()
…
class Purchase_detail(models.Model):
material = models.ForeignKey(Material, on_delete=models.CASCADE)
purchase = models.ForeignKey(Purchase, on_delete=models.CASCADE)
quantity = models.IntegerField()
unit_value = models.IntegerField(default=0)
forms.py
class PurchaseModelForm(forms.ModelForm):
class Meta:
model = Purchase
fields = (‘number’,’date’ , ’…’)
def __init__(self, *args, **kwargs):
super(PurchaseModelForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.form_id = 'id-purchase-form'
self.helper.form_method = 'post'
class Purchase_detailModelForm(forms.ModelForm):
class Meta:
model = Purchase_detail
fields = ('material','quantity','unit_value')
def __init__(self, *args, **kwargs):
super(Purchase_detailModelForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.form_id = 'id-purchase-form'
self.helper.form_method = 'post'
self.helper.form_class = 'form-inline'
self.helper.field_template = 'bootstrap3/layout/inline_field.html'
DetailFormSet = forms.inlineformset_factory(Purchase, Purchase_detail, form=Purchase_detailModelForm, extra=1)
views.py
def purchase_new(request, purchase_id=None, *args, **kwargs):
template = 'erp/purchase_form.html'
if purchase_id:
inst = Purchase.objects.get(pk=purchase_id)
else:
inst = Purchase()
if request.method == 'POST':
form = PurchaseModelForm(request.POST or None, request.FILES, prefix='purchase', instance=inst)
formset = DetailFormSet(request.POST or None, request.FILES, prefix='detail')
form_valid = form.is_valid()
if form_valid:
purchase = form.save()
formset.save(commit=False)
for f in formset:
f.compra = purchase
f.save()
return redirect('...')
else:
form = PurchaseModelForm(prefix='purchase',instance=inst)
formset = DetailFormSet(prefix='purchase')
context = { 'form': form, 'formset': formset }
return render(request, template, context )
这是模板
<form method='POST' action="">
{% csrf_token %}
<div>
<h5 style="font-weight: bold;">Datos de la Compra</h5>
{% crispy form form.helper %}<hr/>
<h5 style="font-weight: bold;">Detalle de la Compra <a class="btn btn-rounded btn-sm btn-icon btn-default add-new-form"><i class="fa fa-plus text-success"></i></a></h5>
</div>
{{ formset.management_form|crispy }}
{% for x in formset %}
<div class="form-inline">
{{ x|crispy }}
<hr>
</div>
{% endfor %}
<div class="form-inline" id="empty-row">
{{ formset.empty_form|crispy }}
<hr>
</div>
<button class="btn btn-block btn-primary" >Guardar</button>
</form>
追溯是: 回溯:
File&#34; /Users/jlattus/Dev/italuzbi/lib/python3.5/site-packages/django/core/handlers/exception.py"在内心 41. response = get_response(request)
File&#34; /Users/jlattus/Dev/italuzbi/lib/python3.5/site-packages/django/core/handlers/base.py"在_get_response中 187. response = self.process_exception_by_middleware(e,request)
File&#34; /Users/jlattus/Dev/italuzbi/lib/python3.5/site-packages/django/core/handlers/base.py"在_get_response中 185. response = wrapped_callback(request,* callback_args,** callback_kwargs)
File&#34; /Users/jlattus/Dev/italuzbi/lib/python3.5/site-packages/django/contrib/auth/decorators.py"在_wrapped_view中 23. return view_func(request,* args,** kwargs)
File&#34; /Users/jlattus/Dev/italuzbi/src/erp/views.py"在purchase_new 415. formset.save(commit = False)
File&#34; /Users/jlattus/Dev/italuzbi/lib/python3.5/site-packages/django/forms/models.py"在保存 666. return self.save_existing_objects(commit)+ self.save_new_objects(commit)
File&#34; /Users/jlattus/Dev/italuzbi/lib/python3.5/site-packages/django/forms/models.py"在save_existing_objects中 768.如果不是self.initial_forms:
File&#34; /Users/jlattus/Dev/italuzbi/lib/python3.5/site-packages/django/forms/formsets.py"在initial_forms中 189. return self.forms [:self.initial_form_count()]
File&#34; /Users/jlattus/Dev/italuzbi/lib/python3.5/site-packages/django/utils/functional.py"在获取 35. res = instance。 dict [self.name] = self.func(instance)
File&#34; /Users/jlattus/Dev/italuzbi/lib/python3.5/site-packages/django/forms/formsets.py"在表格中 144.对于范围内的i(self.total_form_count())]
File&#34; /Users/jlattus/Dev/italuzbi/lib/python3.5/site-packages/django/forms/formsets.py"在total_form_count中 116. return min(self.management_form.cleaned_data [TOTAL_FORM_COUNT],self.absolute_max)
File&#34; /Users/jlattus/Dev/italuzbi/lib/python3.5/site-packages/django/utils/functional.py"在获取 35. res = instance。 dict [self.name] = self.func(instance)
File&#34; /Users/jlattus/Dev/italuzbi/lib/python3.5/site-packages/django/forms/formsets.py"在management_form中 98. code =&#39; missing_management_form&#39;,
异常类型:/ compra / new /的ValidationError 例外价值:[&#39; Los datos de ManagementForm faltan o han sido manipulados