Load Django Form Object with data from Model instance already “loaded”

那年仲夏 提交于 2019-12-10 10:12:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!