OK...I have the following model and form.

*class CmdString(models.Model):
   
    name    = models.CharField(max_length=50)
    cmd     = models.CharField(max_length=200)
    eda_app = models.OneToOneField(EDA_App, primary_key=True)
   
    def __unicode__(self):
        return self.cmd  *  


*class EDA_AppSelectForm(forms.Form):
    app_cmds = forms.ChoiceField(choices=CmdString.objects.all())*

When I execute in a Python shell the command *CmdString.objects.all() *I
have the following output from my database:

*[<CmdString: vcs -a -b -f $FILE1>, <CmdString: vhdlan -c -d -f $FILE2>,
<CmdString: virsim -c -d -f $FILE2>, <CmdString: virt1 -a -b -f $FILE1>,
<CmdString: virt2 -c -d -f $FILE2>, <CmdString: virt3 -c -d -f $FILE2>,
<CmdString: virt4 -c -d -f $FILE2>, <CmdString: quartz1 -c -d -f
$FILE2>, <CmdString: quartz2 -c -d -f $FILE2>, <CmdString: quartz3 -a -b
-f $FILE1>, <CmdString: quartz4 -c -d -f $FILE2>, <CmdString: vcs -a -b
-f $FILE1>, <CmdString: vhdlan -c -d -f $FILE2>, <CmdString: virsim -c
-d -f $FILE2>, <CmdString: virt1 -a -b -f $FILE1>, <CmdString: virt2 -c
-d -f $FILE2>, <CmdString: virt3 -c -d -f $FILE2>, <CmdString: virt4 -c
-d -f $FILE2>, <CmdString: quartz1 -c -d -f $FILE2>, <CmdString: quartz2
-c -d -f $FILE2>, '...(remaining elements truncated)...']*

This is a Python list with each element of the list a dictionary
object.  This list is iterable, i.e.

*for cmd in CmdString.objects.all():
...     print cmd*

will return:

*vcs -a -b -f $FILE1
vhdlan -c -d -f $FILE2
virsim -c -d -f $FILE2
virt1 -a -b -f $FILE1
virt2 -c -d -f $FILE2
virt3 -c -d -f $FILE2
virt4 -c -d -f $FILE2
quartz1 -c -d -f $FILE2
quartz2 -c -d -f $FILE2
quartz3 -a -b -f $FILE1
quartz4 -c -d -f $FILE2
vcs -a -b -f $FILE1
vhdlan -c -d -f $FILE2
virsim -c -d -f $FILE2
virt1 -a -b -f $FILE1
virt2 -c -d -f $FILE2
virt3 -c -d -f $FILE2
virt4 -c -d -f $FILE2
quartz1 -c -d -f $FILE2
quartz2 -c -d -f $FILE2
quartz3 -a -b -f $FILE1
quartz4 -c -d -f $FILE2*

Perfect...I'm happy with this output.

Now, I wanted to take a look at the output of the form to be rendered as
HTML.  I'm looking for the "select" tag with the options to be output
from the "choices" kwarg.

When I to take a look at an instance of this form with "as_p", I get the
following error:

*>>> samp2 = EDA_AppSelectForm()
>>> type(samp2)
<class 'tss_cloud.forms.EDA_AppSelectForm'>
*
*>>> samp2.as_p()
>>> Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File
"/home/jmarcedwards/foobarr/local/lib/python2.7/site-packages/django/forms/forms.py",
line 235, in as_p
    errors_on_separate_row = True)
  File
"/home/jmarcedwards/foobarr/local/lib/python2.7/site-packages/django/forms/forms.py",
line 180, in _html_output
    'field': unicode(bf),
  File
"/home/jmarcedwards/foobarr/local/lib/python2.7/site-packages/django/forms/forms.py",
line 408, in __unicode__
    return self.as_widget()
  File
"/home/jmarcedwards/foobarr/local/lib/python2.7/site-packages/django/forms/forms.py",
line 439, in as_widget
    return widget.render(name, self.value(), attrs=attrs)
  File
"/home/jmarcedwards/foobarr/local/lib/python2.7/site-packages/django/forms/widgets.py",
line 516, in render
    options = self.render_options(choices, [value])
  File
"/home/jmarcedwards/foobarr/local/lib/python2.7/site-packages/django/forms/widgets.py",
line 533, in render_options
    for option_value, option_label in chain(self.choices, choices):
TypeError: 'CmdString' object is not iterable

>>> print samp2.as_p()
>>> Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File
"/home/jmarcedwards/foobarr/local/lib/python2.7/site-packages/django/forms/forms.py",
line 235, in as_p
    errors_on_separate_row = True)
  File
"/home/jmarcedwards/foobarr/local/lib/python2.7/site-packages/django/forms/forms.py",
line 180, in _html_output
    'field': unicode(bf),
  File
"/home/jmarcedwards/foobarr/local/lib/python2.7/site-packages/django/forms/forms.py",
line 408, in __unicode__
    return self.as_widget()
  File
"/home/jmarcedwards/foobarr/local/lib/python2.7/site-packages/django/forms/forms.py",
line 439, in as_widget
    return widget.render(name, self.value(), attrs=attrs)
  File
"/home/jmarcedwards/foobarr/local/lib/python2.7/site-packages/django/forms/widgets.py",
line 516, in render
    options = self.render_options(choices, [value])
  File
"/home/jmarcedwards/foobarr/local/lib/python2.7/site-packages/django/forms/widgets.py",
line 533, in render_options
    for option_value, option_label in chain(self.choices, choices):
TypeError: 'CmdString' object is not iterable*

What is happening here?  When I remove the "choices" kwarg in the
"app_cmds" form field, I can print out the HTML, and the "select" field
is there as expected, but I have no choices/options in the HTML. This is
why I was trying to use the "choices" attribute.

Now the documentation for the "choices" kwarg is pasted below, which
says that this should be an iterable of 2-tuples, but seems to indicate
it could be a list as well.  The documentation here seems unclear, i.e
an iterable of tuple of 2-tuples, I don't think it means this.

How can I achieve what I am looking for here?

> choices
> An iterable _/(e.g., a list or tuple) /_of 2-tuples to use as choices
> for this field. This argument accepts the
> same formats as the choices argument to a model field. See the model
> field reference documentation on
> choices for more details.

Regards, Marc
-- 

J. Marc Edwards
Lead Architect - Semiconductor Design Portals
Nimbis Services, Inc.
Skype: (919) 747-3775
Cell:  (919) 345-1021
Fax:   (919) 882-8602
[email protected]
www.nimbisservices.com

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

<<attachment: marc_edwards.vcf>>

Reply via email to