On Friday, 6 January 2012 15:12:29 UTC, BillB1951 wrote:
>
> I am having trouble passing a parent table record “id” to a new record
> in a child table. My urlconf calls add_childtable in my views.py and
> passes the parenttable_id to it. I have excluded the parenttable from
> my ChildTableForm(ModelForm) because I do not want it to be available
> to the users. When I hit submit – from my form.html I get a
> “KeyError” error message. The parenttable field/column (a ForeighKey
> field) does not want to accept the parenttable_id I am passing to it.
>
> What am I doing wrong? Example code snippets are always appreciated.
>
> Thanks,
> Bill
>
> PS. I notice that the code pasted in below seems to have messed up
> some of the indenting, but its correct in my python files.
>
<snip>
parenttable=form.cleaned_data[parenttable_id],
>
Since parenttable_id isn't coming from the form, this won't work. It's a
local variable, so just
parenttable = parenttable_id
should work.
However you're doing extra work by specifying the fields automatically - a
modelform does that for you:
if form.is_valid():
childtable = form.save(commit=False)
childdtable.parenttable_id = parenttable_id
childtable.save()
Also, next time please remember to provide the actual error message +
traceback when asking for help.
--
DR.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/vma7q0zmTtYJ.
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.