View:
def edit_operation(request, op_id):
from forms import OpBaseForm
from django.forms.models import modelformset_factory
OpBaseFormSet = modelformset_factory(OpBase, form=OpBaseForm)
op = Operation.objects.get(pk=op_id)
formset = OpBaseFormSet(queryset=op.opbase_set.all())
c = RequestContext(request, {'operation': op, 'formset':
formset} )
return render_to_response('operation_edit.html', c )
Template (snipplet):
{% for form in formset.forms %}
<div class="opbase_formbox">
<form>
<table>
{{form}}
</table>
<ul>
{% for route in form.instance.routes.all %}
<li><a href="/edit/route/{{route.pk}}">{{ route }}</
a></li>
{% endfor %}
<li><a href="/new/route/">**Add route**</a></li>
</ul>
</form>
</div>
{% endfor %}
When I try to view this page, I get this error which, oddly appears to
be coming from the very first line of my snipplet, the "{% for form in
formset.forms %}" line...
"Caught an exception while rendering: 'OpBase' instance needs to have
a primary key value before a many-to-many relationship can be used."
If I take out the "{% for route in form.instance.routes.all %}" block,
there is no error at all. Also, if I replace the view with:
def edit_operation(request, op_id):
from forms import OpBaseForm
op = Operation.objects.get(pk=op_id)
formset = []
for opbase in op.opbase_set.all():
formset.append(OpBaseForm(instance=opbase)
c = RequestContext(request, {'operation': op, 'formset':
formset} )
return render_to_response('operation_edit.html', c )
It works fine (except for the fact that my forms all have duplicate
HTML id fields...)
Anyone know whats wrong? If I try to extract formset.forms
[1].instance.routes.all() from the interactive shell it works fine.
Could this be a bug in the template system?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---