Dream_
Dream_

Reputation: 11

Trouble editing a User Model in a view with Flask-Admin

Upon finishing Miguel's Flask Mega tutorial I tried to implement a quick dashboard to access my model entities stored in a SQLite db through a admin page.

One quick way of doing it (according to some) is to use the Flask-Admin extension (with Flask-Login) to roll out Model Views to easily fiddle with them without going through the trouble of implementing a custom web page to display & edit the db entities.

When I try to edit a User within the related view I get the following error :

TypeError: BaseModelView.edit_view() got an unexpected keyword argument 'cls'

Here is my User class :

class User(PaginatedAPIMixin, UserMixin, db.Model):
    id: so.Mapped[int] = so.mapped_column(primary_key=True)
    username: so.Mapped[str] = so.mapped_column(sa.String(64), index=True,
                                                unique=True)
    email: so.Mapped[str] = so.mapped_column(sa.String(120), index=True,
                                             unique=True)
    password_hash: so.Mapped[Optional[str]] = so.mapped_column(sa.String(256))
    posts: so.WriteOnlyMapped['Post'] = so.relationship(back_populates='author')
    about_me: so.Mapped[Optional[str]] = so.mapped_column(sa.String(140))
    last_seen : so.Mapped[Optional[datetime]] = so.mapped_column(
        default=lambda: datetime.now(timezone.utc)
    )
    following: so.WriteOnlyMapped['User'] = so.relationship(
        secondary=followers,
        primaryjoin=(followers.c.follower_id == id),
        secondaryjoin=(followers.c.followed_id == id),
        back_populates='followers'
    )
    followers: so.WriteOnlyMapped['User'] = so.relationship(
        secondary=followers,
        primaryjoin=(followers.c.followed_id == id),
        secondaryjoin=(followers.c.follower_id == id),
        back_populates='following'
    )
    tasks: so.WriteOnlyMapped['Task'] = so.relationship(back_populates='user')
    token: so.Mapped[Optional[str]] = so.mapped_column(
        sa.String(32), index=True, unique=True
    )
    token_expiration: so.Mapped[Optional[datetime]]
    is_admin: so.Mapped[Optional[str]] = so.mapped_column(sa.Boolean, default=False)
    
    def __repr__(self):
        return '<User {}>'.format(self.username)

Here is the related view :

class UserModelView(ModelView):
    def is_accessible(self):
        return current_user.is_authenticated and current_user.is_admin

    def inaccessible_callback(self, name, **kwargs):
        # redirect to login page if user doesn't have access
        return redirect(url_for('auth.login', next=request.url))

Strangely, I can edit & delete the Post Model in its view without any errors.

Can someone tell me what I have been doing wrong ?

Upvotes: 1

Views: 41

Answers (0)

Related Questions