本文介绍了表单错误int()参数必须是字符串或数字,而不是'QueryDict'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人能帮我解决以下错误,并解释问题吗?所有我想要做的是填充一个查询集的组,但是在提交表单时,我收到以下错误... * TypeError at / sms / addbatch
int()参数必须是字符串或数字,而不是QueryDict *
views.py
def add_batch(request):
#如果我们有POST然后获取请求的帖子值。
如果request.method =='POST':
form = BatchForm(request.POST)
#检查我们有有效的数据,然后保存尝试保存。
如果form.is_valid():
#清除所有数据并添加到var数据。
data = form.cleaned_data
groups = data ['group']。split(,)
用于组中的项目:
batch = Batch(content = data [消息'],
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)
如果用户不是None:
form_choices = Batch.objects.for_user_pending(user )
其他:
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)
解决方案
您正在将 request.POST
作为用户
参数到您的表单。这样做:
form = BatchForm(data = request.POST)
#第一个参数--- v
def __init __(self,user = None,...
#first parameter --- v
form = BatchForm(request.POST)
Can someone help me out with the following error below and explain the issue? All I'm trying to do it populate a group with a query set, but upon submitting the form I get the following error...
*TypeError at /sms/addbatchint() argument must be a string or a number, not '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")
解决方案
You are passing request.POST
as the user
parameter to your form. Do this:
form = BatchForm(data=request.POST)
# first parameter ---v
def __init__(self, user=None, ...
# first parameter ---v
form = BatchForm(request.POST)
这篇关于表单错误int()参数必须是字符串或数字,而不是'QueryDict'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!