1

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

1 回答 1

1

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

于 2011-05-01T13:26:15.787 回答