Modify value of a Django form field during clean()

前端 未结 7 1512
陌清茗
陌清茗 2020-12-29 04:42

I am adding custom validation to my forms and custom fields in my Django app. I would like to be able to modify the value of a field when triggering an error. For example, i

7条回答
  •  天命终不由人
    2020-12-29 05:30

    This is the way I've tried and works for me:

     inst = my_form.save(commit=False)
     if not inst.a34_stuff: # or incorrect
         inst.data["a34_stuff"] = "corrected"
    

提交回复
热议问题