使用来自模型实例的数据已“加载”到Django表单对象。 [英] Load Django Form Object with data from Model instance already "loaded"

查看:55
本文介绍了使用来自模型实例的数据已“加载”到Django表单对象。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django中具有以下类型的模板:

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>

视图在其上下文中包含一个ModelForm对象,从中派生模板中的 form变量

The view contains a ModelForm object in its context from which the "form" variable in the template is derived.

通常,您可以传递绑定到诸如request.POST之类的数据的表单。如何加载与模型的特定实例的数据绑定的表单?有什么特殊语法吗?

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?

推荐答案

这里有两个选项:

MyModelForm(instance=myInstanceModel)
MyModelForm(initial={ "my": "initial-data" })

这篇关于使用来自模型实例的数据已“加载”到Django表单对象。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆