I am attempting to use a class-based view to display information about a tool, and then add tags to that view, mostly to teach myself how to use inline_formsets. The problem that I am having is how to inject the child object's form into the template.
The problem is that the child's formsets are appearing, but there is no parent form displaying from the template.
The result is that the parent form doesn't show -
Ultimately, this is a "What am I doing wrong in Django?" question.
The model is very simple - Tool has a few fields that define my parent object, and tag is a child that is related to it:
models.py
from django.db import models
class Tool(models.Model):
content_id = models.CharField(
primary_key=True,
help_text="Confluence ID of the tool document",
max_length=12
)
tool_name = models.CharField(
max_length=64,
help_text="Short name by which the tool is called",
)
purpose = models.TextField(
help_text="A one sentence summary of the tools reason for use."
)
body = models.TextField(
help_text="The full content of the tool page"
)
last_updated_by = models.CharField(
max_length=64
)
last_updated_at = models.DateTimeField()
def __unicode__(self):
return u"%s content_id( %s )" % (self.tool_name, self.content_id)
class ToolTag(models.Model):
description = models.CharField(
max_length=32,
help_text="A tag describing the category of field. Multiple tags may describe a given tool.",
)
tool = models.ForeignKey(Tool)
def __unicode__(self):
return u"%s describes %s" % (self.description, self.tool)
I am using standard Class-based forms:
forms.py
from django.http import HttpResponseRedirect
from django.views.generic import CreateView
from django.views.generic import DetailView, UpdateView, ListView
from django.shortcuts import render
from .forms import ToolForm, TagsFormSet
from .models import Tool
TagsFormSet = inlineformset_factory(Tool, ToolTag, can_delete='True')
class ToolUpdateView(UpdateView):
template_name = 'tools/tool_update.html'
model = Tool
form_class = ToolForm
success_url = 'inventory/'
views.py
def call_update_view(request, pk):
form = ToolUpdateView.as_view()(request,pk=pk)
tag_form = TagsFormSet()
return render( request, "tools/tool_update.html",
{
'form': form,
'tag_form': tag_form,
'action': "Create"
}
)
And my template is as follows:
tool_update.html
{% block content %}
<form action="/update/" method="post">
{% csrf_token %}
<DIV>
Tool Form:
{{ form.as_p }}
</DIV>
<DIV>
Tag Form:
{{ tag_form.as_p }}
</DIV>
<input type="submit" value="Submit" />
</form>
{% endblock %}