my template looks like:
<form method="post" action="">
{{ formset.management_form }}
{% for form in formset.forms %}
{{ form.contractor }} {{ form.date }} {{ form.value }} {{ form.comment }} {{ form.operation_type }} {{ form.category }} {{ form.account }}
{% endfor %}
</form>
but the result allows to change all of fields – but i want only one.
I thought that (please notice “.value” after all but category field) solves the problem, but not.
<form method="post" action="">
{{ formset.management_form }}
{% for form in formset.forms %}
{{ form.contractor.value }} {{ form.date.value }} {{ form.value.value }} {{ form.comment.value }} {{ form.operation_type.value }} {{ form.category }} {{ form.account.value }}
{% endfor %}
</form>
UPD:
relevant view code
def detail(request, category_id):
from django.forms.models import modelformset_factory
OperationFormSet = modelformset_factory(Operation)
if request.method == "POST":
formset = OperationFormSet(request.POST, request.FILES,
queryset=Operation.objects.filter(category=category_id))
if formset.is_valid():
formset.save()
# Do something.
else:
formset = OperationFormSet(queryset=Operation.objects.filter(category=category_id))
return render_to_response("reports/operation_list.html", {
"formset": formset,
})
Your problem is that the readonly values are NOT passed back to the server, and thus your formset should fail as it’s only receiving one field.
You’d want to show the value AND set a hidden field to store the data the formset is expecting. But I’d recommend a different approach…
Ultimately, a formset and form is used to display html forms and not built for anything else. You’d have to hack it to show hidden widgets, make sure users can’t POST arbitrary data, etc. So instead, I would use the forms framework to only display your one editable field.
Create a
ModelFormthat only has one editable fieldAs for displaying values in the template, ModelForms should have their instance directly accessible via
form.instanceso you should be able to do something like this: