V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
coolair
V2EX  ›  问与答

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

  •  
  •   coolair · Apr 17, 2018 · 1230 views
    This topic created in 2938 days ago, the information mentioned may be changed or developed.
        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 了,而是值。

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1003 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 17:59 · PVG 01:59 · LAX 10:59 · JFK 13:59
    ♥ Do have faith in what you're doing.