问题
I have the following type of template in Django:
<form .... >
<label class="control-label" for="id_quantity">Quantiy</label>
{{ form.quantity }}
<label class="control-label" for="id_title">Quantiy</label>
{{ form.title}}
</form>
The view contains a ModelForm object in its context from which the "form" variable in the template is derived.
Often, you can pass a form that is bound to data like request.POST. How do I load a form that is bound to the data of a specific instance of a model? Is there any special syntax?
回答1:
There are two options here:
MyModelForm(instance=myInstanceModel)
MyModelForm(initial={ "my": "initial-data" })
来源:https://stackoverflow.com/questions/11896096/load-django-form-object-with-data-from-model-instance-already-loaded