开发者

How to save a field added in the clean() method?

My form has first_name and last_name fields, but my model has just a name field. In the clean() method of the form I set self.cleaned_data['name'], but when I try to save it I get this error:

<ul class="errorlist"><li>name<ul class="errorlist"><li>This field is required.</li开发者_JAVA百科></ul></li></ul>

If I exclude it,

class Meta:
    model = UserProfile
    exclude = ('name',)

however, then it doesn't get saved.

So how do I get it to both save, and not throw an error?


Adding

name = CharField(required=False)

To my form, but not displaying it on the page seems to work. I realized it was failing the first wave of validation before it even got to the clean() method, which is why it erroring. Just had to set required=False.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜