我希望你能看到我想在这里做什么,只是我想迭代组看起来像group = 1,3,5等的值并将它们添加到数据库中。组是一个复选框。所以我想使用拆分选项。我收到以下消息寿....Django'QuerySet'对象没有属性'拆分'
“查询集”对象有没有属性“分裂”
所以这是我的理解它是在我使用的填充这是一种查询形式的init,我需要这个,但在发布后,它应该只是一个列表。我做错了什么?
view.py
form = BatchForm(request.user, 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 form.cleaned_data['group']:
batch = Batch(content=data['content'],
group=Group.objects.get(pk=item),
user=request.user
)
batch.save()
forms.py
class BatchForm(forms.ModelForm):
class Meta:
model = Batch
exclude = ('user', 'group')
def __init__(self, user=None, *args, **kwargs):
super(BatchForm, self).__init__(*args,**kwargs)
if user is not None:
form_choices = Group.objects.for_user(user)
else:
form_choices = Group.objects.all()
self.fields['group'] = forms.ModelMultipleChoiceField(
queryset=form_choices
)
template.py
{% for value, text in form.group.field.choices %}
<input type="checkbox" name="group" value="{{ value }}" /> {{text}}<br />
{% endfor %}