我有一个名为 Question 的模型。模型允许用户创建新问题。我正在尝试使用对象的查询集填充多个表单。当我尝试使用查询集进行初始化时,就会出现问题。我收到这个错误
'Question' object is not iterable
File "C:\mysite\pet\views.py" in DisplayAll
294. formset = form(initial=q)
模型.py
class Question(models.Model):
question= models.CharField(max_length=500)
user = models.ForeignKey(User)
形式
class QuestionForm(forms.ModelForm):
question= forms.CharField(required=True,max_length=51)
class Meta:
model = Question
fields = ('question',)
意见
def DisplayAll(request):
q = Question.objects.filter(user=request.user)
form = formset_factory(QuestionForm)
formset = form(initial=q)
return render(request,'question.html',{'formset':formset})
模板
{% for f in formset %}
{{f}}
{% endfor %}