How to determine if a field has changed in a Django modelform

后端 未结 4 1352
甜味超标
甜味超标 2021-01-04 07:25

I was surprised that this was difficult to do. However I came up with this, which seems to work at least for my simple case. Can anyone recommend a better approach?

4条回答
  •  暖寄归人
    2021-01-04 07:59

    Extending on Airs's answer, for multiple fields:

    In a scenario where you'd like to track changes for a list of fields ['field_a', 'field_b', 'field_c']


    If you'd like to check if any of those fields has changed:

    any(x in myforminstance.changed_data for x in ['field_a', 'field_b', 'field_c'])
    

    If you'd like to check if all of those fields have changed:

    all(x in myforminstance.changed_data for x in ['field_a', 'field_b', 'field_c'])
    

提交回复
热议问题