小编Pet*_*rth的帖子

如何确定Django模型中的字段是否已更改

我很惊讶这很难做到.但是我想出了这个,这似乎至少对我的简单案例起作用.有谁能推荐更好的方法?

def field_changed(self, fieldname):
    """Tests if the value of the field changed from the original data"""
    orig_value = self.fields[fieldname].initial or getattr(self.instance, field, None)
    orig_value = getattr(orig_value, 'pk', orig_value)
    if type(orig_value) is bool:
        # because None and False can be interchangeable
        return bool(self.data.get(fieldname)) != bool(orig_value)
    else:
        return unicode(self.data.get(fieldname)) != unicode(orig_value)
Run Code Online (Sandbox Code Playgroud)

django django-forms

7
推荐指数
2
解决办法
9977
查看次数

React js生命周期方法不会在包装组件上触发

这是一个组件:

export default class MyComponent extends React.Component {
  componentWillReceiveProps(newProps) {
    console.log('RECEIVED PROPS');
  }

  render() {
    return <div>{this.props.foo}</div>
  }
}
Run Code Online (Sandbox Code Playgroud)

这是一个包装器/更高阶组件:

  const withSomething = (ComponentToWrap) => {
    render() {
      return <ComponentToWrap {...this.props} />
    }
  }
Run Code Online (Sandbox Code Playgroud)

这是一个功能组件,它将MyComponent包装在withSomething中:

export default function WrappedComponent(props) {
    const Component = withSomething(MyComponent);
    return <Component ... some props ... />
}
Run Code Online (Sandbox Code Playgroud)

结果:即使我更新道具,MyComponent中与道具相关的生命周期函数(例如componentWillReceiveProps)也不会触发.

这怎么了?基于道具的生命周期方法不适用于包装组件吗?

javascript reactjs

4
推荐指数
1
解决办法
932
查看次数

标签 统计

django ×1

django-forms ×1

javascript ×1

reactjs ×1