How To Populate A Dropdown List

703 views
Skip to first unread message

hank23

unread,
Feb 8, 2011, 11:03:25 AM2/8/11
to Django users
I have coded a form which will display some data in a dropdown
selection box. The data is being populated from a a queryset that I
have setup in the form's code. However the entries in the dropdown
only display as objects of the table from which they're being
retrieved, and don't display the actual field data that I was hoping
to have it display. So how do I specify in the form which of the table
fields is to be the display field and which is suppoed to be the
underying key value to be passed when and entry is selected? Thanks
for the help.

SimpleDimple

unread,
Feb 8, 2011, 6:19:53 PM2/8/11
to Django users
I am new to django so not sure if I am of much help but let me try

the key is usually the ID field of your table.
for the value to display add a method __str___ in your model, here is
sample code from one of my project
read more on ___str___ and ___unicode___ methods


class Teacher(models.Model):
school = models.ForeignKey("School")
xclass = models.ForeignKey("Class", verbose_name='Class')
name = models.CharField("Teacher Name",max_length=50)

def __str__(self):
return self.name

Brian Neal

unread,
Feb 8, 2011, 9:47:28 PM2/8/11
to Django users
Post your code somewhere, e.g. dpaste.com so we have a better idea
what you are trying to do.

Best,
BN

SimpleDimple

unread,
Feb 9, 2011, 3:54:13 PM2/9/11
to Django users
did it work Hank ?

Mac

unread,
Feb 10, 2011, 12:54:34 AM2/10/11
to Django users
Here's some sample code that works for me:

in models.py
class InventoryCloseouts(models.Model):
prim = models.IntegerField(primary_key=True, db_column='PRIM') #
Field name made lowercase.
items = models.CharField(max_length=255, db_column='ITEMS',
blank=True) # Field name made lowe
gen = models.CharField(max_length=255, db_column='GEN',
blank=True) # Field name made lowercase.
descrip = models.CharField(max_length=255, db_column='DESCRIP',
blank=True) # Field name made l
.......... etc.
def __unicode__(self):
return self.items + ":" + self.gen+":"+self.descrip

in forms.py
class InvtForm2(forms.Form): #used in closeout order view.
invent=InventoryCloseouts.objects.all()
items = forms.ModelMultipleChoiceField(queryset=invent,
widget=forms.Select(attrs={'class':'colr', }))

in views.py
def order_closeouts(request):


return render_to_response('order_closeouts.html', {'forms':forms},
context_instance = RequestContext(request))

Malcolm MacKinnon

unread,
Feb 10, 2011, 12:58:46 AM2/10/11
to Django users
I forgot to initialize the form in my view. See views.py below.

Here's some sample code that works for me:

in models.py
class InventoryCloseouts(models.Model):
   prim = models.IntegerField(primary_key=True, db_column='PRIM') #
Field name made lowercase.
   items = models.CharField(max_length=255, db_column='ITEMS',
blank=True) # Field name made lowe
   gen = models.CharField(max_length=255, db_column='GEN',
blank=True) # Field name made lowercase.
   descrip = models.CharField(max_length=255, db_column='DESCRIP',
blank=True) # Field name made l
     .......... etc.
   def __unicode__(self):
        return self.items + ":" + self.gen+":"+self.descrip

in forms.py
class InvtForm2(forms.Form):   #used in closeout order view.
   invent=InventoryCloseouts.objects.all()
   items = forms.ModelMultipleChoiceField(queryset=invent,
             widget=forms.Select(attrs={'class':'colr', }))

in views.py
def order_closeouts(request):
    forms=InvtForm2()
      ............etc.

    return render_to_response('order_closeouts.html', {'forms':forms},
       context_instance = RequestContext(request))

On Wed, Feb 9, 2011 at 9:54 PM, Mac <mmac...@gmail.com> wrote:


On Feb 8, 8:03 am, hank23 <hversem...@stchas.edu> wrote:
> I have coded a form which will display some data in a dropdown
> selection box. The data is being populated from a a queryset that I
> have setup in the form's code. However the entries in the dropdown
> only display as objects of the table from which they're being
> retrieved, and don't display the actual field data that I was hoping
> to have it display. So how do I specify in the form which of the table
> fields is to be the display field and which is suppoed to be the
> underying key value to be passed when and entry is selected? Thanks
> for the help.

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


Malcolm MacKinnon

unread,
Feb 10, 2011, 1:12:11 AM2/10/11
to Django users
And one more file to render the drop down list, order_closeouts.html below. Sorry about the messy code. It ain't pretty but it works for me. Let me know if you have any questions. 
in order_closeouts.html

<div>{{forms.items}}<span id="selection"><--Select Item from complete list</span></div>
Reply all
Reply to author
Forward
0 new messages