关于模型实例的Django模型表错误

时间:2021-10-02 15:53:27

Trying to instance a model form in django, I am passing in an instance and I get validation errors out, despite the data being entered from the very same form minutes prior. ignore the extra fields stuff I have tried removing it and the issue persists. It adds extra fields from a separate key value table.

尝试在django中实例化一个模型表单,我正在传递一个实例,但我得到了验证错误,尽管数据是从几分钟之前的相同表格输入的。忽略我试图删除它的额外字段的东西,问题仍然存在。它从单独的键值表中添加额外的字段。

Django Version 1.3

Django版本1.3

def edit(request,company_uuid,contact_uuid,address_uuid):
    mcompany = get_object_or_404(models.Company, uuid = company_uuid)
    extra_questions, extra_ids = get_questions(request, 2)
    if request.method == 'POST': # If the form has been submitted...
        form = forms.EditCompanyForm(request.POST,instance=mcompany, extra=extra_questions, extra_ids=extra_ids)
        if form.is_valid():
            fModel = form.save(commit = False)
            for (question, answer) in form.extra_answers():
                save_answer(request, question, answer, mcompany.uuid, 2)
            form.save()    
            url = reverse('contacts_details',kwargs={'company_uuid':mcompany.uuid,
                                                                   'address_uuid':address_uuid,
                                                                   'contact_uuid':contact_uuid})
            return HttpResponseRedirect(url)
    else:
        form = forms.EditCompanyForm(request.POST,instance=mcompany, extra=extra_questions, extra_ids=extra_ids)
    return share.output_page(request,'contacts/edit.html',{'form': form,
                                                            'company_uuid':mcompany.uuid,
                                                            'address_uuid':address_uuid,
                                                            'contact_uuid':contact_uuid})  

class EditCompanyForm(jsforms.JMSModelForm):
    """
    The Company form with the contacts fields included
    """
    name = forms.CharField(label = _('Company Name'),widget = forms.TextInput(attrs={'class':'large-input-box'}))
    discount=forms.FloatField(widget = jsforms.StandardUnit("PC"),required=False)
    allow_download = forms.BooleanField(required=False,label=_('Allow download'))

    def __init__(self, *args, **kwargs):

        extra = kwargs.pop('extra')
        extra_id = kwargs.pop('extra_ids')
        super(EditCompanyForm, self).__init__(*args, **kwargs)

        for i, question in enumerate(extra):
            settings = ExtraFieldSetup.objects.get(uuid=extra_id[i])
            if settings.type == 2:
                list = []
                list_partial = str(settings.extra).split(':')
                for choice in list_partial:
                    x = choice, choice
                    list.append(x)
                self.fields['custom_%s' % i] = forms.ChoiceField(choices=list, label=question, required=False)
            else:
                self.fields['custom_%s' % i] = forms.CharField(label=question, required=False)

    def extra_answers(self):
        for name, value in self.cleaned_data.items():
            if name.startswith('custom_'):
                yield (self.fields[name].label, value)

    class Meta:
        model = models.Company
        exclude = ('company', 'otherdetails', 'jms_code', 'logo', 'hidden', 'user')

1 个解决方案

#1


1  

You're passing in request.POST even in the GET stage of the view (after the else), when the POST dictionary will be empty, so naturally there will be validation errors.

你甚至在视图的GET阶段(在else之后)传递request.POST,当POST字典为空时,自然会出现验证错误。

#1


1  

You're passing in request.POST even in the GET stage of the view (after the else), when the POST dictionary will be empty, so naturally there will be validation errors.

你甚至在视图的GET阶段(在else之后)传递request.POST,当POST字典为空时,自然会出现验证错误。