Friday, January 20, 2012

Re: How to use a DB table for choices=tuples in ChoiceField?

Thank you, Dennis.  I'll study your answer.  I have to do this kind of thing for a lot of different tables.  And I discovered another thing:  In the template I was reading the table in again from the database!  Before my problem was solved, I could populate the selection list from the DB table, but then there were no valid choices when I got back to the view.

I'm getting to know Django just enough that I can sometimes answer questions posted here, or at least make suggestions.

Regards

On Fri, Jan 20, 2012 at 11:22 PM, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
On Fri, 20 Jan 2012 19:44:16 -0800 (PST), Bill Beal
<b.beal@eximflow.com> wrote:

       Caveat: I've only looked at Django, but not in enough detail to be
"expert"; my comments are based upon basic Python syntax/semantics (my
apologies to the developers, but I work better with printed
documentation and Django is too much of a moving target for the few
books that did come out to be relevant four months later)

>name 'scac_choicelist' is not defined at the marked
>line in my form:
>
       Which is true -- it is not defined at that point. (NOTE: your names
don't really help understanding the purpose of the variables <G>)

>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

       Presuming the many "-"s represent indentation, "scac_choicelist" is
a method of Scac. That means it should have at least one parameter: self

       def scac_choicelist(self):
               scaclist = []
               for item in self.objects.all():
                       scaclist.append((item.code, item.name))
               return scaclist

NOTE: based on your "-"s, your return statement is inside your loop, so
you'd be returning after processing only one element.

       NOW... In the form module you need to reference an instance of the
model! The method itself is not visible.

>- scac = forms.ChoiceField(
>----- label=u'Ocean Carrier',
>----- choices=scac_choicelist()) ######
>

Something like:

       scac = forms.ChoiceField(       label=u"Ocean Carrier",
                                                               choices=Scac().scac_choicelist())

--
       Wulfraed                 Dennis Lee Bieber         AF6VN
       wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

--
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.


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