Hi All,
When I looked in the docs for how to fill in the
"choices=" attribute for a ChoiceField in a form,
I could only find static sets of tuples for examples.
I need to use a table from the DB for the tuples.
I thought I could make up the choice list myself
from the table, but I'm getting a Name Error:
name 'scac_choicelist' is not defined at the marked
line in my form:
from django import forms
from django.forms.fields import ChoiceField
from myapp.models import *
BOL_CODE_QUALIFIERS = (
('OB', 'Ocean Bill of Lading (OB)'),
('BM', 'House Bill of Lading (BM)'),
)
class BolForm(forms.Form):
- scac = forms.ChoiceField(
----- label=u'Ocean Carrier',
----- choices=scac_choicelist()) ######
- bill_of_lading = forms.CharField(
----- label=u'Bill of Lading #', max_length=50)
- bol_type = forms.ChoiceField(
----- label=u'Type of B/L',
----- choices=BOL_CODE_QUALIFIERS)
Here's my model:
from django.db import models
class Scac(models.Model):
- code = models.CharField(max_length=4)
- name = models.CharField(max_length=60)
- def __unicode__(self):
--- return self.code
- def scac_choicelist():
--- q = Scac.objects.all()
--- scaclist = []
--- for x in q:
----- scaclist.append((x.code,
x.name))
----- return scaclist
I didn't know where to stick the scac_choicelist()
code, so I put it with the model.
Questions:
1. Is this a reasonable approach? If not, what?
2. If so, how can I get scaclist into the form?
I hope there is enough info here that my mistake
is obvious to somebody.
Thanks.