class BolForm(forms.Form):
- q = Scac.objects.all()
- scaclist = []
- for x in q:
--- scaclist.append((x.code, x.name))
- scac = forms.ChoiceField(
----- label=u'Ocean Carrier', choices=scaclist)
- 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)
It worked. But is there a better way?
On Fri, Jan 20, 2012 at 10:44 PM, Bill Beal <b.beal@eximflow.com> wrote:
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.
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment