Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Django Form Object with data from Model instance already "loaded"

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?

like image 781
goelv Avatar asked Nov 18 '25 19:11

goelv


1 Answers

There are two options here:

MyModelForm(instance=myInstanceModel)
MyModelForm(initial={ "my": "initial-data" })
like image 122
Jonas Geiregat Avatar answered Nov 20 '25 09:11

Jonas Geiregat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!