Django:创建一个不设置所有必需模型字段的表单?

时间:2011-05-01 07:17:43

标签: django google-app-engine django-forms djangoappengine

使用django / appengine,我如何呈现允许输入某些模型字段的表单,然后在代码中设置其他模型字段。调用form.save(commit = False)会引发异常,因为未设置用户。正确的Django处理这种情况的方法是什么?

我有以下模型和表单类:

class FooModel(db.Model):
    user = db.UserProperty(required=True)
    date = db.DateProperty(required=True)

class FooForm(djangoforms.ModelForm):
    class Meta:
        model = models.FooModel
        exclude = ['user']

以下请求处理程序代码:

if request.method == 'POST':
    form = forms.FooForm(request.POST)
    # form.save() raises an exception because 'user' is not set
    foo = form.save(commit=False)
    foo.user = users.get_current_user()
    foo.save()

1 个答案:

答案 0 :(得分:1)

要匹配SDK example,您可以从UserProperty中省略required=True