Hi there,

Instead of using the course object in your initial data, which will
pass in the __unicode__ representation of the object, pass in the id:

form = CouseBook(initial = {'course': course.id})

That should get you the numeric id, but you'll also need to override
your save method to get the course object to assign when you save your
CourseBook form, as you can't assign an integer (coming from your
hidden form field) to the value of a ForeignKey field on a model.

HTH,
Brandon

On Mar 31, 8:20 am, phoebebright <phoebebright...@gmail.com> wrote:
> 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 post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to