Having trouble passing a parent table record “id” to a new record in a child table

16 views
Skip to first unread message

BillB1951

unread,
Jan 6, 2012, 10:12:29 AM1/6/12
to Django users
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.




# models.py

class ChildTable(models.Model):
field1 = models.IntegerField(null=True, blank=True)
field2 = models.CharField(max_length=35, blank=True)
field3 = models.CharField(max_length=35, blank=True)
parenttable = models.ForeignKey(ParentTable)


class ChildTableForm(ModelForm):
class Meta:
model = ChildTable
exclude = ('parenttable',)

# views.py

def add_childtable(request, parenttable_id):
if request.method == 'POST':
form = ChildTableForm(request.POST)
if form.is_valid():
# create a new listing
childtable = ChildTable.objects.create(
field1=form.cleaned_data['field1'],built'],
field2=form.cleaned_data['field2'],built'],
field3=form.cleaned_data['field3'],built'],
parenttable=form.cleaned_data[parenttable_id],
)
# Always redirect after a POST
return http.HttpResponseRedirect('/appdir/childtable/%s/'
% childtable.id)
else:
# This the the first page load, display a blank form
form = ChildTableForm()
context = Context({'title': 'Add ChildTableData', 'form': form})
context.update(csrf(request))
return render_to_response('appdir/form.html', context)

# form.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>{{ title }}</h1>
<form method='post' action='.'> {% csrf_token %}
<!-- display the form fields -->
{{ form.as_p }}

<!-- submit button -->
<input type='submit'>
</form>
</body>
</html>


Andre Terra

unread,
Jan 6, 2012, 11:01:46 AM1/6/12
to django...@googlegroups.com
I introduce to you django-mptt: "utilities for implementing a modified pre-order traversal tree in django".

https://github.com/django-mptt/django-mptt
http://django-mptt.github.com/django-mptt/


Cheers,
AT



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Daniel Roseman

unread,
Jan 6, 2012, 11:35:35 AM1/6/12
to django...@googlegroups.com


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.
Reply all
Reply to author
Forward
0 new messages