The Questions object is not iterableable by Django

I have a model called "Question". The model allows users to create new questions. I am trying to fill out several forms with task objects. The problem occurs when I try to initialize using a set of queries. I get this error

'Question' object is not iterable File "C:\mysite\pet\views.py" in DisplayAll 294. formset = form(initial=q) 

models.py

 class Question(models.Model): question= models.CharField(max_length=500) user = models.ForeignKey(User) 

forms

 class QuestionForm(forms.ModelForm): question= forms.CharField(required=True,max_length=51) class Meta: model = Question fields = ('question',) 

views

 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}) 

template

  {% for f in formset %} {{f}} {% endfor %} 
+8
django
source share
1 answer

Initial expects a dictionary of values, so you just need to modify your query like this:

 q = Question.objects.filter(user=request.user).values() 

See docs on values()

+15
source share

All Articles