Django - 覆盖cleaned_data
我有一个表单,其中的字段不在相应模型上。我使用这些“虚拟”字段通过 clean() 方法填充真实字段。
因此,用户在“虚拟”字段中输入数据,我必须用相同的数据填充真实字段。
我认为覆盖 clean_data["real_field"] 是可能的,但我做不到。
我的代码是这样的:
(...)
cleaned_data['real_field'] = cleaned_data['virtual_field']
(...)
return cleaned_data
关于我可以做的另一种方式的任何想法,或者如果我做错了,我该如何修复它?
I have a form with fields that are not on the correspondent model. I use these "virtual" fields to fill a real one with the clean() method.
So, the user enters with data in the "virtual" field and I have to fill the real field with this same data.
I thought that overriding the cleaned_data["real_field"] would be possible, but I cannot do it.
My code is something like this:
(...)
cleaned_data['real_field'] = cleaned_data['virtual_field']
(...)
return cleaned_data
Any ideas on another way I can do it, or if I am doing it wrong, how do I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在你的表单类中:
In your form class:
没关系,我有一个错误。真实字段未在字段集中声明(admin.py)。
Nevermind, I had an error. The real field was not declared in the fieldsets (admin.py).