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 On Fri, Jan 6, 2012 at 1:12 PM, BillB1951 <wjbur...@gmail.com> 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. > > > > > # 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> > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@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. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.