Here's a nicer way to do it. CHOICES = list ( (obj.code,obj.name) for obj in Scac.objects.all() )
Or you can always make a static method for it and call Scac.objects_as_list() El 21 de enero de 2012 05:01, Bill Beal <b.b...@eximflow.com> escribió: > It looks like the Pythonic way is to create the list in > models.py and invoke it from forms.py. > > Sorry about the heterogeneous names--some of > them match the document the requirements were > extracted from and others are more meaningful > to me. The former were picked because we'll > need a requirements cross-reference and they'll > be easier to match up with the requirements. > And the dashes in place of spaces--most of the > code examples I see here are almost unreadable > because of leading spaces disappearing. > I saw one example of replacing all but the last > leading space with '-', and it was more readable. > > Regards > > On Fri, Jan 20, 2012 at 11:22 PM, Dennis Lee Bieber <wlfr...@ix.netcom.com > > wrote: > >> On Fri, 20 Jan 2012 19:44:16 -0800 (PST), Bill Beal >> <b.b...@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 >> wlfr...@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+unsubscr...@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+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- Regards, Leandro Ostera, *BLOG - Check what I'm doing now <http://imakeapps.alwaysdata.net> EMAIL - Write me an email <leost...@gmail.com> SHOWCASE - Check my latest projects <http://ishowcase.alwaysdata.net>* -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.