Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I turn a string model field into a select input in Flask-Admin?

I have a string field in my SQLAlchemy model and I would like to expose a select box with a few options in Flask-Admin instead of the standard text field.

class MyModel(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    my_field = db.Column(db.String(128))  # Field I would like to be choices


class MyModelView(ModelView):
    """
    Admin manager for MyModel
    """

    # Which option should I use here?

    def __init__(self):
        super(MyModelView, self).__init__(MyModel, db.session)
like image 886
nickromano Avatar asked Jun 26 '15 20:06

nickromano


2 Answers

It ended up being a combination of form_overrides and form_args. form_overrides tells the form to use a select field and form_args allows you to pass choices and other options.

class MyModel(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    my_field = db.Column(db.String(128))


class MyModelView(ModelView):
    """
    Admin manager for MyModel
    """

    form_overrides = dict(
        my_field=SelectField
    )
    form_args = dict(
        my_field=dict(
            choices=[
                ('choice_1', 'Choice 1'),
                ('choice_2', 'Choice 2')
            ]
        )
    )
    def __init__(self):
        super(MyModelView, self).__init__(MyModel, db.session)
like image 165
nickromano Avatar answered Nov 07 '22 03:11

nickromano


You achieve this functionality by using 'form_choices' in the model view as the following example based on the code embedded in your question illustrates:

   class MyModel(db.Model):
       id = db.Column(db.Integer, primary_key=True)
       my_field = db.Column(db.String(128))  # Field I would like to be choices


    class MyModelView(ModelView):
        """
        Admin manager for MyModel
        """

            form_choices = {
                 'my_field': [
                     ('choice_1', 'Choice 1'),
                     ('choice_2', 'Choice 2'),
                     ('choice_3', 'Choice 3'),
                     ('choice_4', 'Choice 4'),
                     ('choice_5', 'Choice 5')
                ]
           }

        def __init__(self):
            super(MyModelView, self).__init__(MyModel, db.session)

Reference:

Flask-Admin documentation:

You can restrict the possible values for a text-field by specifying a list of select choices:

form_choices = { 'title': [ ('MR', 'Mr'), ('MRS', 'Mrs'), ('MS', 'Ms'), ('DR', 'Dr'), ('PROF', 'Prof.') ] }

like image 40
Angelos Avatar answered Nov 07 '22 05:11

Angelos