title: Using a list to populate model form foreign key in django
body: <p>I am trying to restrict my modelform foreign key instances using a list:</p>
  
  <p>here is my model:</p>
  
  <pre><code>class Voicemail(models.Model):
      internalline = models.ForeignKey(InternalLine)
      person = models.ForeignKey(Person)
  </code></pre>
  
  <p>Then in init method of a corresponding model form I have</p>
  
  <pre><code>class VoicemailForm(ModelForm):
   def __init__(self, *args, **kwargs):
    ....
    ....
   # self.fields['internalline'].queryset = self.person.getPersonLines()  # this throws error in template.
     self.fields['internalline'].queryset = self.person.line.all()
  
     print(self.person.getPersonLines())
     print(self.person.line.all())
  </code></pre>
  
  <p>The two prints print identical output:</p>
  
  <pre><code>[&lt;InternalLine: 1111&gt;, &lt;InternalLine: 5555&gt;]
  [&lt;InternalLine: 1111&gt;, &lt;InternalLine: 5555&gt;]
  </code></pre>
  
  <p>In getPersonLines, I do some additional logic and return a list of lines:</p>
  
  <pre><code>def getPersonLines(self):
      lines = []
      for line in self.line.order_by('extension'):
          lines.append(line)
      for phone in self.phonesst_set.all():
          for line in phone.line.order_by('extension'):
              if line not in lines:               
                  lines.append(line)   
  
      return lines
  </code></pre>
  
  <p>Now when I try to render in template, if I use the list returned by getPersonLines, I get and error,</p>
  
  <pre><code>Caught AttributeError while rendering: 'list' object has no attribute 'all'
  </code></pre>
  
  <p>But the same thing works if I populate the queryset using self.person.line.all()..</p>
  
  <p>Am I missing something while trying to populate my queryset using a list?</p>
  
  <p>Thanks in advance!!</p>
  
  <p><strong>Update</strong></p>
  
  <p>Here is the stack trace with </p>
  
  <pre><code>self.fields['internalline'].choices = self.person.getPersonLines()  
  
  Traceback (most recent call last):
  File "&lt;console&gt;", line 1, in &lt;module&gt;
  File "C:\Python26\lib\site-packages\django\utils\encoding.py", line 27, in __str__
      return self.__unicode__().encode('utf-8')
  File "C:\Python26\lib\site-packages\django\forms\forms.py", line 95, in __unicode__
      return self.as_table()
  File "C:\Python26\lib\site-packages\django\forms\forms.py", line 217, in as_table
      errors_on_separate_row = False)
  File "C:\Python26\lib\site-packages\django\forms\forms.py", line 180, in _html_output
     'field': unicode(bf),
  File "C:\Python26\lib\site-packages\django\forms\forms.py", line 408, in __unicode__
     return self.as_widget()
  File "C:\Python26\lib\site-packages\django\forms\forms.py", line 439, in as_widget
     return widget.render(name, self.value(), attrs=attrs)
  File "C:\Python26\lib\site-packages\django\forms\widgets.py", line 516, in render
     options = self.render_options(choices, [value])
  File "C:\Python26\lib\site-packages\django\forms\widgets.py", line 533, in render_options
  for option_value, option_label in chain(self.choices, choices):
  TypeError: 'InternalLine' object is not iterable
  </code></pre>
  
  <p><strong>Update 2:</strong></p>
  
  <p>This is the entire <code>__init__</code> method</p>
  
  <pre><code>class VoicemailForm(ModelForm):
  
  def __init__(self, *args, **kwargs):
     try: 
          self.person = kwargs.pop('person', None)
          super(VoicemailForm, self).__init__(*args, **kwargs)
          for field in self.fields:
              self.fields[field].widget.attrs['class'] = 'required_field'
  
          print(self.person.getPersonLines())
          print(self.person.line.all())
          if self.person:
              self.fields['internalline'].choices = self.person.getPersonLines()  
              #self.fields['internalline'].queryset = self.person.line.all()
  
     except Exception as e:
         print("Error overwriting __init__ of VoicemailForm")
         print(e)    
  </code></pre>
  
  <p>This is how I call my form </p>
  
  <pre><code>voicemail = VoicemailForm(person=person, prefix='voicemail')
  </code></pre>
  
  <p><strong>Update 3</strong>
  I tried to create a django form as follows:</p>
  
  <pre><code>class test(forms.Form):
   line = forms.ChoiceField()
   def __init__(self, *args, **kwargs):
          super(test, self).__init__(*args, **kwargs)
          self.fields['line'].choices=person.getPersonLines()
  </code></pre>
  
  <p>But I continue to get the same error
  <code>for option_value, option_label in chain(self.choices, choices):
  TypeError: 'InternalLineSST' object is not iterable</code></p>
  
  <p>Then I tired something like:</p>
  
  <pre><code>test = [1,2,3]
  class myform(forms.Form):
      line = forms.ChoiceField()
      def __init__(self, *args, **kwargs):
          super(myform, self).__init__(*args, **kwargs)
          self.fields['line'].choices = test
  
  &gt;&gt;&gt; form = myform()
  &gt;&gt;&gt;print(form)
  </code></pre>
  
  <p>which gives me a similar error</p>
  
  <pre><code>File "C:\Python26\lib\site-packages\django\forms\forms.py", line 439, in as_widget
   return widget.render(name, self.value(), attrs=attrs)
  File "C:\Python26\lib\site-packages\django\forms\widgets.py", line 516, in render
   options = self.render_options(choices, [value])
  File "C:\Python26\lib\site-packages\django\forms\widgets.py", line 533,in render_options
  for option_value, option_label in chain(self.choices, choices):
  TypeError: 'int' object is not iterable`
  </code></pre>
  
  <p>Am I missing something here?</p>