创建计算字段并立即以表格形式显示的最佳做法是什么?
示例:
import * as React from 'react';
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {
date: new Date()
}
}
componentDidMount() {
this.props.handleTime(this.state.date);
}
render() {
return <View>{this.state.date.toString()}</View>;
}
}
当我为测试模式添加新记录时,我想在Extra_price字段中显示评估值export class Home extends Component {
displayName = Home.name
state = {
time: null
};
handleTime = (timeValue) =>
this.setState(currentState => ({
time: timeValue
})
);
render() {
return (
<div>
<div>
<Clock time={this.handleTime} />
</div>
</div>
);
}
}
。
我该怎么办?
谢谢
答案 0 :(得分:0)
一种简单而优雅的解决方案是在模型中定义一个property()
并在模型管理员中使用
class User(models.Model):
id = models.AutoField(db_column='Id', primary_key=True) # Field name made lowercase.
price = models.IntegerField(db_column='Price')
extra_field = models.DecimalField(db_column='extra_field')
@property
def my_logic(self):
return self.price * 123456 # return whatever you want
@register(Test)
class TestAdmin(admin.ModelAdmin):
readonly_fields = ['my_logic']