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

时间:2013-03-22 10:50:03

标签: django django-forms

有人可以帮助我解决以下错误并解释问题吗?我正在尝试用查询集填充一个组,但在提交表单后,我收到以下错误...

/ pms / addbatch上的

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

views.py

def add_batch(request):
    # If we had a POST then get the request post values.
    if request.method == 'POST':
        form = BatchForm(request.POST)
        # Check we have valid data before saving trying to save.
        if form.is_valid():
            # Clean all data and add to var data.
            data = form.cleaned_data
            groups = data['group'].split(",")
            for item in groups:
                batch = Batch(content=data['message'],
                              group=Group.objects.get(pk=item),
                              user=request.user
                              )

form.py

class BatchForm(forms.ModelForm):


    class Meta:
        model = Batch


    def __init__(self, user=None, *args, **kwargs):
        super(BatchForm, self).__init__(*args,**kwargs)
        if user is not None:
            form_choices = Batch.objects.for_user_pending(user)
        else:
            form_choices = Batch.objects.all()

        self.fields['group'] = forms.ModelMultipleChoiceField(
            queryset=form_choices
        )

models.py

class BatchManager(models.Manager):
    def for_user_pending(self, user):
        return self.get_query_set().filter(user=user, status="Pending")

1 个答案:

答案 0 :(得分:2)

您将request.POST作为user参数传递给表单。这样做:

form = BatchForm(data=request.POST)

#  first parameter ---v
def __init__(self, user=None, ...

#  first parameter ---v
form = BatchForm(request.POST)