I have a ForeignKey field in a model of mine, and I’m using ModelForm with it to generate the HTML. The thing is, I want to add an Other option as well – I plan to add JavaScript with it so that a textbook appears when that’s selected.
I’m looking at http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets, and started off with trying something like
class Event(models.Model):
name = models.CharField(max_length=200)
time = models.DateField()
.
.
.
cost = models.CharField(max_length=200)
affiliation = models.ForeignKey('Affiliation')
def __unicode__(self):
return self.name
class EventForm(ModelForm):
cost = models.TextField()
class Meta:
model = Event
cost should become a text field instead of char field, so the output should be textarea instead of input[type=text]. This doesn’t change, however, no errors are printed, and nothing really happens. I was hoping to proceed by doing
class EventForm(ModelForm):
affiliations = list(Affiliation.objects.all()).append('Other')
affiliation = forms.CharField(choices=affiliations)
class Meta:
model = Event
I’m using django-nonrel on GAE if it helps, but this isn’t really an issue with the Model (or so I think…) so I don’t think it should change anything. Any help would be much appreciated!
I haven’t used django-nonrel, so take this tip with a grain of salt (YMMV).
In your
EventFormdefinition, you’re settingcostto be amodel.TextField– but you actually want it to be aforms.CharField, with a textarea widget.