如何以 django 形式将初始值传递给图像字段
目前我正在使用类似的东西:
initialValues={
'textField1':'value for text field 1',
'textField2':'value for text field 2',
'imageField': someModel.objects.get(id=someId).logo
}
form = myForm(initial=initialValues)
当我如上所述调用 myForm 时,初始值按预期显示:textField1、textField2 和 imageField(带有当前选项:linkToImage、清除复选框和更改:)
但是当我保存表单时, imageField 字段中没有保存任何内容(检查数据库,我看到 imageField 字段为空)。
我知道我在这里错过了一些东西,但我不知道是什么。有什么建议吗?
Currently I am using something like:
initialValues={
'textField1':'value for text field 1',
'textField2':'value for text field 2',
'imageField': someModel.objects.get(id=someId).logo
}
form = myForm(initial=initialValues)
When I call myForm as above, the initial values are displayed as expected: the textField1, textField2 and imageField (with the options Currently: linkToImage, Clear check box and Change: )
But when I save the form, there is nothing saved in the imageField field (checking the database and I see the imageField field blank).
I know that I miss something here, but I cannot figure out what. Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过
在保存表单之前进行分配解决了我的问题。耶伊
I solved my issue by assigning
just before I save the form. Yeey
创建表单时需要将数据传入表单。
如果您还没有这样做,我建议使用 基于类视图。使用 FormView 您可以轻松覆盖 get_initial_data( ) 函数,指定您的值,然后让视图负责传递给表单以保存它的其他内容。如果您想保存模型(我想您就是这样),请查看 CreateView 和 UpdateView。
如果我确切地知道您如何/何时初始化该表单,我可以更加确定这个答案。
You need to pass in the data to the form when creating it.
If you're not doing so already, I would suggest using class-based views. With a FormView you can easily override the get_initial_data() function, specify your values, and then let the view take care of what other things to pass on to the form to save it. If you're trying to save a model (which I think you are), check out the CreateView and UpdateView.
I could be more sure about this answer if I knew exactly how/when you were initializing that form.