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.
On Jun 30, 11:45 am, Daniel Roseman <[email protected]> wrote:
> On Thursday, 30 June 2011 18:51:54 UTC+1, sony wrote:
>
> > 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.
>
> 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.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.