开发者

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

开发者 https://www.devze.com 2023-03-21 11:27 出处:网络
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:

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

精彩评论

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

关注公众号