liveBook · Manning

UserMixin

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

UserMixin is a Flask-Login mixin class that supplies standard methods and properties expected of a login-managed user object.

A database-backed user needs to do two jobs: store account data and work with the login system. A Flask `User` class can inherit from both `UserMixin` and `db.Model`. `UserMixin` supplies methods expected by Flask-Login’s `LoginManager`; `db.Model` supplies the SQLAlchemy functionality for defining and accessing the user table. section 8.2.1

class User(UserMixin, db.Model):                                          #1
    __tablename__ = "user"                                                #2
    user_uid = db.Column(db.String, primary_key=True, default=get_uuid)   #3
    first_name = db.Column(db.String, nullable=False)                     #4
    last_name = db.Column(db.String, nullable=False)                      #4
    email = db.Column(db.String, nullable=False,                          #4
    unique=True, index=True)                                            #4
    hashed_password = db.Column("password",                               #4
    db.String, nullable=False)                                          #4
    active = db.Column(db.Boolean, nullable=False, default=False)         #4
    created = db.Column(db.DateTime, nullable=False,                      #5
    default=datetime.utcnow)                                            #5
    updated = db.Column(db.DateTime, nullable=False,                      #5
    default=datetime.utcnow, onupdate=datetime.utcnow)                  #5
 
    def get_id(self):
        return self.user_uid
 
    @property
    def password(self):
        raise AttributeError("user password can’t be read")
 
    @password.setter
    def password(self, password):
        self.hashed_password = generate_password_hash(password)
 
    def verify_password(self, password):
        return check_password_hash(self.hashed_password, password)
 
    def __repr__(self):
        return f"""
        user_uid: {self.user_uid}
        name: {self.first_name} {self.last_name}
        email: {self.email}
        active: {'True' if self.active else 'False'}
        """

The `User` model inherits from both classes and defines account fields, including a UUID primary key named `user_uid`. section 8.2.1

Check the identifier before relying on the mixin unchanged. Its default `get_id()` uses `self.id`, but this model names its unique identifier `user_uid`. Override `get_id()` to use that field so the login system can identify the user. section 8.2.1

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Explains multiple inheritance and the need to override get_id(), without detailing the other mixin methods.
Examples
Shows a User model inheriting from UserMixin and db.Model, with a UUID field named user_uid.
What it assumes
Builds on a Flask application with LoginManager and SQLAlchemy while explaining the roles of the two inherited classes.
What you can do after
Apply it via Flask-Login to equip a User model for login management and adapt get_id() to a custom identifier.
How it connects
Connects UserMixin to Flask-Login, LoginManager, SQLAlchemy models, multiple inheritance, and UUID primary keys.

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.