Re: Having trouble passing a parent table record “id” to a new record in a child table
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. > 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 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.
Re: Having trouble passing a parent table record “id” to a new record in a child table
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, BillB1951wrote: > 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 > > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd;> > http://www.w3.org/1999/xhtml;> > > {{ title }} > > > {{ title }} > {% csrf_token %} > > {{ form.as_p }} > > > > > > > > > -- > 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.
Having trouble passing a parent table record “id” to a new record in a child table
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 http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd;> http://www.w3.org/1999/xhtml;> {{ title }} {{ title }} {% csrf_token %} {{ form.as_p }} -- 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.