本文介绍了如何以模型形式给出初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为ModelForm内部的字段分配初始值?例如:

How do I assign initial values to fields inside a ModelForm? eg:

class Form1(forms.Form):
    title=forms.CharField(initial="hello")

使用modelForm的基本语法类似于:

What will be the equivalent for this using modelForm whose basic syntax is something like:

class Form2(djangoforms.ModelForm):
    class Meta:
        model=SomeModel
        fields=('title',)

我要执行的操作是生成CRUD。由于我是在Appengine专案中进行的,因此无法使用一般检视。 Appengine向我们提供了 djangoforms.ModelForm ,其功能与django的 ModelForm 一样,但是使用了appengine的数据存储区。

What I am trying to do is generate a CRUD. Since I am doing it in an appengine project I can't use generic views. Appengine has provided us djangoforms.ModelForm which works just like the django's ModelForm but uses appengine's datastore.

我需要上述功能来执行编辑部分。

I need the above functionality to do the "edit" part.

推荐答案

通常,您会将您正在编辑的模型对象作为实例关键字arg传递给以下形式:
Form2(instance = somemodelobject),但我不知道它是否适用于GAE。
您总是可以将初始字典传递给表单的构造函数,例如

Normally you would pass the model object you are editing as instance keyword arg to the form:Form2(instance = somemodelobject), but I don't know if it works on GAE.You can always pass initial dictionary to your form's constructor, like

Form2(initial = {"title": "blahblah"})

这篇关于如何以模型形式给出初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:31