使用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()