Drop down box in django getting populated from a database table column

1,606 views
Skip to first unread message

sony

unread,
Jun 30, 2011, 1:51:54 PM6/30/11
to Django users
I searched the same thing on net and got many answers but some how
dint get working with any of them. Table: 1. Report :
reportType(foreign key from ReportCategory), name, description 2.
Report Category: name,description forms.py

class ReportForm_insert(forms.ModelForm):

class Meta:
model=Report
invent = ReportCategory.objects.all()
print invent
reportType_id = forms.ModelMultipleChoiceField(queryset = invent)

fields =('name','description',)

model.py

class ReportCategory(models.Model):

name = models.CharField(max_length=20)

description = models.CharField(max_length=20)

def __unicode__(self):
return self.name

class Report(models.Model):

reportType = models.CharField(max_length=200)

name = models.CharField(max_length=200)

description = models.CharField(max_length=300)

def __unicode__(self):
return self.name

I want the ReportType to be shown as a drop down box which would
contain values from the name column of the Report Category table. I am
surely going wrong big way but I am not understanding what to do... So
I am not understanding whether there is a problem with not able to
handle the foreign key or the drop down box.

Daniel Roseman

unread,
Jun 30, 2011, 2:45:29 PM6/30/11
to django...@googlegroups.com
It's not so much "going about it the wrong way" as "posting completely nonsensical code". You certainly didn't see anywhere "on the net" anything that told you to write logic in the Meta inner class.

You don't have a ForeignKey in your model. If you want the reportType field to be populated with elements from the ReportCategory model, you should make it a ForeignKey.
--
DR.

sony

unread,
Jun 30, 2011, 4:42:57 PM6/30/11
to Django users
I am sorry I think I posted one line wrong.

class Report(models.Model):
reportType = models.ForeignKey(ReportCategory)
name = models.CharField(max_length=200)
description = models.CharField(max_length=300)

def __unicode__(self):
return self.name

Now, inside the meta class I am trying to do two things:

Firstly, populating the Report Type dropdown box with the value from
the 'name' column of ReportCategory table.
Secondly, when all the other fields in the form are filled by the user
and the button is pressed, the data in the fields should be saved in
the Report table keeping in mind the foreign key constraint it has
from the ReportCategory table.

Daniel Roseman

unread,
Jun 30, 2011, 5:03:22 PM6/30/11
to django...@googlegroups.com
Op donderdag 30 juni 2011 21:42:57 UTC+1 schreef sony het volgende:
I am sorry I think I posted one line wrong.

class Report(models.Model):
reportType = models.ForeignKey(ReportCategory)
name = models.CharField(max_length=200)
description = models.CharField(max_length=300)

def __unicode__(self):
    return self.name

Now, inside the meta class I am trying to do two things:

Firstly, populating the Report Type dropdown box with the value from
the 'name' column of ReportCategory table.
Secondly, when all the other fields in the form are filled by the user
and the button is pressed, the data in the fields should be saved in
the Report table keeping in mind the foreign key constraint it has
from the ReportCategory table.

But you don't need to do anything to get that - that is the default behaviour.

In any case, even if you did need to do custom logic, the place for that is *not* in Meta. __init__ might be a good place. But, as I said, all the things you want are the standard way forms behave with ForeignKeys.
--
DR. 

sony

unread,
Jun 30, 2011, 5:10:08 PM6/30/11
to Django users
Ok...this might work for the second option .
How about getting a drop down box from a data base table column

Daniel Roseman

unread,
Jun 30, 2011, 5:17:38 PM6/30/11
to django...@googlegroups.com
Op donderdag 30 juni 2011 22:10:08 UTC+1 schreef sony het volgende:
Ok...this might work for the second option .
How about getting a drop down box from a data base table column

For the third time: that is what automatically happens when you use a ForeignKey field in a form. No programming required.
--
DR.

sony

unread,
Jun 30, 2011, 6:32:51 PM6/30/11
to Django users
Thanks a lot...I understood...and its working
provided me a complete different angle of looking at it :)
Reply all
Reply to author
Forward
0 new messages