0

有人可以帮我解决以下错误并解释问题吗?我所做的只是用查询集填充一个组,但是在提交表单时我收到以下错误...

* /sms/addbatch int() 参数中的 TypeError 必须是字符串或数字,而不是 'QueryDict' *

视图.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
                              )

表格.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
        )

模型.py

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

1 回答 1

2

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

form = BatchForm(data=request.POST)

 

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

#  first parameter ---v
form = BatchForm(request.POST)
于 2013-03-22T10:51:23.637 回答