int()参数必须是字符串或数字,而不是'QueryDict'

时间:2011-07-11 11:35:14

标签: django django-forms

我在表单上渲染了3个多选框。 我正在过滤每个盒子以分开一天。 当我提交表单时,我收到此错误。

int() argument must be a string or a number, not 'QueryDict'

我该怎样做才能保存表单?

这就是我在forms.py文件中执行的操作,以便为每个选择框进行不同的过滤。

class ContractForm(forms.ModelForm):

    def __init__(self, project_id, *args, **kwargs):
        super(ContractForm, self).__init__(*args, **kwargs)
        self.fields['shoot_day'].queryset = Day.objects.filter(type=SHOOT, project__id=project_id)
        self.fields['travel_day'].queryset = Day.objects.filter(type=TRAVEL, project__id=project_id)
        self.fields['additional_day'].queryset = Day.objects.filter(type=ADDITIONAL, project__id=project_id)

我正在获取我的project_id:

def editcontract(request, contract_id, slug):
    context_dict = {}
    contract = get_object_or_404(Contract, pk=contract_id)
    if request.method == 'POST':
        form = ContractForm(request.POST, instance=contract)
        if form.is_valid():
            form.save()
            TvUsageForm = TvUsageFormSet(request.POST, instance=contract)
            AdditionalMediaUsageForm = AdditionalMediaUsageFormSet(request.POST, instance=contract)
            TvUsageForm.save()
            AdditionalMediaUsageForm.save()
            return  HttpResponseRedirect(reverse('contract_list', kwargs={'slug':slug}))
    else:
        form = ContractForm(instance=contract, project_id=contract.project_id)
        TvUsageForm = TvUsageFormSet(instance=contract)
        AdditionalMediaUsageForm = AdditionalMediaUsageFormSet(instance=contract)
        project = get_object_or_404(Project, slug=slug)
        context_dict = { 'form': form,
                         'tvusage_form':TvUsageForm,
                         'additional_form':AdditionalMediaUsageForm,
                         'project':project
        }
    return render_to_response('contracts/edit_contract.html', context_dict, RequestContext(request))

2 个答案:

答案 0 :(得分:7)

您对参数列表和关键字参数感到困惑:

而不是:

ContractForm(instance=contract, project_id=contract.project_id)

你想:

ContractForm(contract.project_id, instance=contract)

详细说明:您的构造函数接受项目id作为第一个参数而不是关键字参数。因此,您需要将其作为第一个参数。简单的困惑,嗯?

答案 1 :(得分:0)