Because CharField is designed for text data. So the form field turns the course into a string and tries to save a string to the model. That doesn’t work.
The only built-in field designed to work with foreign keys is ModelChoiceField. You could update it so that it doesn’t run a queryset and uses HiddenInput. The other option is to remove course from the ModelForm and instead use course_id with IntergerField. This would be the fastest and not hit the database as often. I recommend doing this. Or, if this uses an existing object, not have anything there. Things left out of the ModelForm don’t get updated when you save the form. > On May 6, 2016, at 9:22 AM, Michel Z. Santello <michel...@gmail.com> wrote: > > Can somebody explain why did occurs with widget=forms.HiddenInput ? > > Thank's. > > > > > Em quarta-feira, 31 de março de 2010 10:20:48 UTC-3, Phoebe Bright escreveu: > Displayed fields resolve as expected, hidden fields cause errors. > This works: > > in the model > CourseBook has a foreign key to Course > > In the view: > > course = Course.objects.get(pk=whatever) > form = CouseBook(initial = {'course': course}) > > > in the Form: > > class CourseBook(ModelForm): > class Meta: > model = CourseBooking > > > The web page now displays a picklist of courses with the initial value > highlighted. I can now do a form.save() no problem. > > However, if I make the course a hidden field, which is what I want. > > class CourseBook(ModelForm): > course = forms.CharField(widget=forms.HiddenInput()) > > class Meta: > model = CourseBooking > > then when I come to save() I get a ValueError, unable to assign "My > course" etc. as it tries to put the name of the course into the > foreign key instead of a course instance. > > I can work around this putting a save method on the form, but it seems > to me django should resolve this for me???? > > > > > > > > > > > > > > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at https://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/3628bd01-59b2-4291-94f5-072d6de9f7c7%40googlegroups.com. > For more options, visit https://groups.google.com/d/optout. Peter of the Norse rahmc...@radio1190.org -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20DEC617-798B-4178-9640-B96D254785AC%40Radio1190.org. For more options, visit https://groups.google.com/d/optout.