Question

I have model like this:

class User(db.Model):
    __tablename__ = 'users'
    __table_args__ = {'mysql_engine' : 'InnoDB', 'mysql_charset' : 'utf8'}
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)
    _password = db.Column('password', db.String(80))

    def __init__(self, username = None, email = None, password = None):
        self.username = username
        self.email = email
        self._set_password(password)

    def _set_password(self, password):
        self._password = generate_password_hash(password)

    def _get_password(self):
        return self._password

    def check_password(self, password):
        return check_password_hash(self._password, password)

    password = db.synonym("_password", descriptor=property(_get_password, _set_password))

    def __repr__(self):
        return '<User %r>' % self.username

I have ModelView:

class UserAdmin(sqlamodel.ModelView):
    searchable_columns = ('username', 'email')
    excluded_list_columns = ['password']
    list_columns = ('username', 'email')
    form_columns = ('username', 'email', 'password')

But no matter what i do, flask admin didn't show password field when i'm editing user info. Is there any way ? Even just to edit hash code.

UPDATE: https://github.com/mrjoes/flask-admin/issues/78

Was it helpful?

Solution

Reason why it did not work - Flask-Admin was not able to figure out what to do with SynonymProperty, so it failed to generate form field.

There's a way you can have it working right now:

class UserAdmin(sqlamodel.ModelView):
    searchable_columns = ('username', 'email')
    excluded_list_columns = ['password']
    list_columns = ('username', 'email')
    form_columns = ('username', 'email')

    def scaffold_form(self):
        form_class = super(UserAdmin, self).scaffold_form()
        form_class.password = wtf.TextField('Password')
        return form_class

I pushed simple fix which adds support for the SynonymProperty, so it will work even without form customization. Unfortunately, I'm in process of adding MongoDB backend, so I won't be able to release new version any time soon.

Just in case, SynonymProperty was superseded by hybrid properties in SQLAlchemy 0.7 and onward, which should be supported by the Flask-Admin.

OTHER TIPS

Much better:

from wtforms.fields import PasswordField

class UserAdmin(sqlamodel.ModelView):
    searchable_columns = ('username', 'email')
    excluded_list_columns = ['password']
    form_overrides = dict(password=PasswordField)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top