Tuesday, October 11, 2011

noob struggling with inlineformset_factory

Hello,

I am new to Python and Django and struggling with the concept of
inlineformset_factories.

I am trying to have a view that nests one form inside another. Consider
these models:

class PersonModel(models.Model):
name = models.CharField(max_length=255)
job = models.ForeignKey(JobModel)

class JobModel(models.Model):
description = models.CharField(max_length=255)

I want to display a set of nested forms, where the "job" field of
"PersonModel" corresponds to a form based on "JobModel." I am attaching
an image of what I mean.

However, using the default behavior:

class PersonForm(forms.ModelForm):
class Meta:
model = PersonModel

I wind up with the "job" field corresponding to a combobox.

I thought that using an inlineformset_factory could solve my problem,
but this code:

def index(request):
PersonFormSet = inlineformset_factory(JobModel,PersonModel)
return render_to_response('person/index.html, {'formset' :
PersonFormSet, })

with this template:

<form action="." method="POST">
<table>
{{ formset.as_table }}
</table>
<p><input type="submit" value="Submit"></p>
</form>

doesn't work; It displays the "name" field but just ignores the "job"
field. And it I provide the keyword argument "extra" to the
inlineformset_factory function, the view displays that many copies of
the "name" field. Clearly, I don't understand something.

Any ideas on what I'm doing wrong? Should I not be using an inlineformset?

Many thanks for your help.

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment