开发者

Django: Creating a form that does not set every required model field?

Using django/appengine, how can I present a form which allows entry of certain model fields, and then set additional model fields in code. Calling form.save(commit=False) raises an exception because user is not set. What the correct Django way to handle cases such as this?

I have the following model and form classes:

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

class FooForm(djangoforms.ModelForm):
    class Meta:
    开发者_如何学Go    model = models.FooModel
        exclude = ['user']

And the following request handler code:

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


To match the SDK example, you could omit required=True from your UserProperty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜