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
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 Cou
OK - Now I've actually read your code properly! Yes that is a
brilliant solution!
Thanks.
Phoebe.
On Apr 1, 7:01 pm, Nuno Maltez wrote:
> I think the form I sent, with ModelChoiceField, will validate with the
> returned values without any further code. Or am I missing something
> obvious?
>
>
I think the form I sent, with ModelChoiceField, will validate with the
returned values without any further code. Or am I missing something
obvious?
On Thu, Apr 1, 2010 at 6:49 PM, phoebebright wrote:
> That's one option, the problem is changing the course value returned
> from the form into a co
That's one option, the problem is changing the course value returned
from the form into a course object so that the form will validate.
This turned out to be a surprising amount of trouble (because you have
to do it before validation), hence reason for doing simpler work
around.
On Apr 1, 2:08 pm,
What about:
class CourseBook(ModelForm):
course = ModelChoiceField(queryset=Course.objects.all(),
widget=HiddenInput())
class Meta:
model = CourseBooking
and in your view:
form = CourseBook(initial = {'course': course.pk})
Nuno
On Wed, Mar 31, 2010 at 8:06 PM, phoebebright wrot
Final approach:
in the forms.py excluded the courses foreign key field
in models.py made courses blank and nullable
didn't pass any initial values to the form
in the view, saved the form like this:
item = Courses.objects.get(pk=whatever)
obj = form.save(commit=False)
Brandon, Thanks for your suggestion.
I tried passing it an ID, but as you say, I also have to override the
save. What I don't understand is why it does it fine if the form
includes the foreign key in a popup? They are both passing back
integers after all.
Also failed to get the save method worki
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 c
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:
10 matches
Mail list logo