flasksqlalchemyflask-admin

How to load choices for one of form_extra_fields in ModelView dynamically during serving data for rendering?


I have an User admin ModelView with form_extra_field constituency and I wanted to have it dynamically loaded choices (it is SelectField). Model for this view is User which will store constituency as id of the item.

I tried to load choices from remote API but certainly it was wrong time to load these things because of RuntimeError: Working outside of application context. It happened at migration command ...venv/bin/flask db init -d ctiweb/migrations . I read some API_KEY from config object which needs to have application context ...

So solution would be load choices at serving or rendering time. I don't want have the solutiont in customized template rather I want it in customized admin ModelView for User model.

Is there such a solution? Can I overload somemethod of ModelView which has application context already (so it is during serving data or rendering template)?

part of source code from models.py

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password_hash = db.Column(db.String(128))
    email = db.Column(db.String(255), nullable=False)
    is_superuser = db.Column(db.Boolean())
    constituency_id = db.Column(db.Integer())

    def __str__(self):
        return self.username

    @property
    def password(self):
        raise AttributeError("not readable attribute")

    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)

part of source code from admin.py

from flask_admin.contrib.sqla import ModelView

class UserAdminModelView(ModelView):

    def serve_constituency_choices():
        consituencies = (
            get_constituency(constituency_id) for constituency_id in get_constituencies()
        )
        return ((constituency['id'], constituency['name']) for constituency in consituencies)

    column_list = ['username', 'email', 'is_superuser']
    form_columns = ['username', 'email', 'password', 'is_superuser', 'constituency']
    form_extra_fields = {
        'password': PasswordField('Password'),
        'constituency': SelectField('Constituency', choices=serve_constituency_choices())
    }

Solution

  • Actually I have finally find a simpler solution - only with class iterator on constituency choices. I think is better than the first one :-)

    class ConstituencyChoices(Iterator):
        def __init__(self, first_item=None):
            self.first_item = first_item
            self.constituencies = None
    
        def __next__(self):
            if self.first_item is not None:
                res = self.first_item
                self.first_item = None
                return res
            if self.constituencies is None:
                self.constituencies = (
                    get_constituency(constituency_id) for constituency_id in get_constituencies()
                )
            constituency = self.constituencies.__next__()
            res = (constituency['id'], constituency['name'])
            return res
    
    # pylint: disable=too-many-ancestors
    class UserAdminModelView(ModelView):
    
        column_list = ['username', 'email', 'is_superuser']
        form_columns = ['username', 'email', 'password', 'is_superuser', 'constituency']
        form_extra_fields = {
            'password': PasswordField('Password'),
            'constituency': SelectField(
                'Constituency',
                coerce=int,
                choices=ConstituencyChoices(
                    (0, "--- {} ---".format(_('no constituency')))
                )
            )
        }