开发者

Manipulate the form field values before rendering in django admin

开发者 https://www.devze.com 2023-04-07 02:13 出处:网络
I have to actually reset the values of the fields of a model Form, every time the form is rendered. So, lets say everytime a user fills the form, I store the values to a different table and next time

I have to actually reset the values of the fields of a model Form, every time the form is rendered. So, lets say everytime a user fills the form, I store the values to a different table and next time when the form is rendered the values are reset to specified data. Again, when the user saves the fo开发者_开发知识库rm, the values are added to the different table and so no. This is quite weird but I need to implement this.

So, how am I supposed to reset the values of a model form before the form is displayed. Definitely, the model belonging to that form is updated with the new values everytime. But I need to reset them when rendered again for edit.

Let me pinpoint what I actually need. I need to override the field values during edit. So if a model was saved with field A having value 'value1'. I need to change this field's value to 'specified value' during edit. So even if the user then changes it to value 2 and saves it. During edit, I again want to have the value set to 'specified value' in the rendered form. It is independent of the values in the database


You can set form values using initial value:

form = MyForm(initial={'field1': value1, 'field2': value2, 'field3': value3})
0

精彩评论

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