Hi all,
I've been working fruitlessly all day attempting what I though would
be a simple task: Adding a sub form/inline form so that I can have two
related forms displaying on the same page tied to models in a one-to-
many relationship.
This was very easy to do in the admin, but now I am attempting to do
this on my pages aimed at users.
I have been searching this mailing list, and google, all day and have
come up with very little. I read through the form chapter in Apress
Practical Django Projects (chapter 9) and unbeleivbly in that book
inline forms/sub forms are not covered outside of the Admin. In fact,
when I tend to find any information it is usually somebody trying to
extend the functionality in the admin, which I am not trying to do.
What I did come up with was to find inlineformset_factory. This seems
to be what i'm after, but it's proving tricky to find a clear and
simple example of its usage, in a context relating to my project,
anywhere on the web.
To give you a more specific idea of what im trying to implement:
############## High level aim: #########################
The are projects, and one project can have many issues and many
schedule items. For example:
Computer upgrade project:
issues:
john needs to order computers
bill needs to look into comparability issues
jane needs to order new internet circuit
schedule:
jan 16th orders
jan 24th delivery
jan 25th installation
jan 30th training
################# My models: ############################
class Project(models.Model):
name = models.CharField(max_length=50)
start_date = models.DateField(null=True, blank=True)
scheduled_end = models.DateField(null=True, blank=True)
is_live = models.BooleanField(default=True)
company = models.ForeignKey('Company')
date_created = models.DateField(auto_now_add=True)
last_updated = models.DateField(auto_now=True)
class Meta:
ordering = ['company']
def __unicode__(self):
return '%s %s' % (
self.name, self.company)
class Project_schedule(models.Model):
start_date = models.DateField(null=True, blank=True)
duration = models.IntegerField(null=True, blank=True)
project = models.ForeignKey('Project')
project_role = models.ForeignKey('Project_role')
date_created = models.DateField(auto_now_add=True)
last_updated = models.DateField(auto_now=True)
class Meta:
ordering = ['project']
def __unicode__(self):
return '%s %s' % (self.project_role, self.project)
class Project_issue(models.Model):
name = models.CharField(max_length=50)
priority = models.ForeignKey('Priority_choice')
owner = models.CharField(max_length=50, null=True, blank=True)
notes = models.TextField(null=True, blank=True)
logged_date = models.DateField(null=True, blank=True)
date_for_resolution = models.DateField(null=True, blank=True)
project = models.ForeignKey('Project')
date_created = models.DateField(auto_now_add=True)
last_updated = models.DateField(auto_now=True)
class Meta:
ordering = ['project']
def __unicode__(self):
return '%s %s' % (
self.name, self.project)
############## My attempt at a view: ###############
#project view
def project(request):
if request.method == 'POST':
#form = ProjectForm(request.POST)
form = inlineformset_factory(Project, Project_schedule,
request.POST )
if form.is_valid():
form.save()
return HttpResponseRedirect('')
else:
form = inlineformset_factory(Project, Project_schedule,
extra=0)
return render_to_response('sam_app/project.html', {'form':
form})
############################
I understand that the above must be totally incorrect. I'm not getting
any data showing up on my template.
I found this resource:
http://markmail.org/message/7byspr32sbarh7ni#query:inlineformset_factory+page:1+mid:zfow7vn6n5bkakmp+state:results
Which says something about the formset being a class and needing to
instantiate it before passing it to the template, but I am not able to
follow this so well.
My template looks like this:
{% block content%} <form action="." method="POST">
<table>
{{ form.as_table }}
</table>
<p><input type="submit" value="Submit"></p>
</form>
{% endblock %}
I'd really appreciate it if somebody could point me to a resource
which gives an example of what i'm trying to accomplish. I imagine
this must be a common requirement so I just can't understand why I can
find anything after so much searching.
Thanks,