V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
coolair
V2EX  ›  问与答

flask-admin 重写字段为 SelectField 如何保存 int 值?

  •  
  •   coolair · 2018-04-17 17:10:22 +08:00 · 963 次点击
    这是一个创建于 2370 天前的主题,其中的信息可能已经有所发展或是发生改变。
        column_choices = {
            'state': [(0, 'OK'), (1, 'Failed'), (2, 'Unknown')]
        }
    
        form_overrides = dict(
            state=SelectField
        )
    
        form_args = dict(
            state=dict(coerce=int, choices=[(0, 'OK'), (1, 'Failed'), (2, 'Unknown')], validators=[Optional()])
        )
    

    想让 state 这个字段保存 0,1,2 这样的整形,但是在列表上展示的时候显示对应 choice 的 text。

    我按上面这样写,列表刷新直接报错

    TypeError: 'NoneType' object is not iterable

    但是改成

    choices=[('0', 'OK'), ('1', 'Failed'), ('2', 'Unknown')]

    就没有问题,不过列表页就不能显示 choice 对应的 text 了,而是值。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2751 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 11:14 · PVG 19:14 · LAX 04:14 · JFK 07:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.