Search code examples
flaskflask-admin

Flask Admin create user?


I am trying to build a web app that only certain users can log in to. As such while I have a registration page on my boilerplate app, I would like to disable it and use flask admin to create and manage users.

However, when I log in to the admin page certain fields like email and password are not available. How do I get these fields into the create tab on the admin menu? See below user model.

class User(db.Model, UserMixin):

    __tablename__ = 'users'

    first_name = db.Column(db.String)
    last_name = db.Column(db.String)
    email = db.Column(db.String, primary_key=True)
    confirmation = db.Column(db.Boolean)
    _password = db.Column(db.String)
    email_confirmation_sent_on = db.Column(db.DateTime, nullable=True)
    email_confirmed_on = db.Column(db.DateTime, nullable=True)
    registered_on = db.Column(db.DateTime, nullable=True)
    last_logged_in = db.Column(db.DateTime, nullable=True)
    current_logged_in = db.Column(db.DateTime, nullable=True)

Solution

  • I don't know why you aren't seeing an email field, since you haven't included the definition of your Users admin view, but parameters that start with an underscore (like _password) will be automatically hidden by flask-admin. You could explicitly include it in your User admin class:

    class UserAdminView(BaseModelView):
        ...
        form_columns = (..., '_password', ...)
        ...
    

    If regenerating or migrating your database is possible, you can also just remove the underscore from your model.

    Edit: It looks like you can also declare ignore_hidden = False on the user admin view.