[Python] Fastadmin does not record the registration of the class model

Discussão em 'Python' iniciado por Stack, Maio 28, 2025.

  1. Stack

    Stack Membro Participativo

    My user model inherits from fastapi-users lib. When I try to log into the admin panel I get an error "User model is not registered."

    from fastapi_users.db import SQLAlchemyBaseUserTable
    from .mixins import IntIdPkMixin, CompanyRelationMixin
    from core.types.user_id import UserIdType

    class User(
    Base,
    IntIdPkMixin,
    CompanyRelationMixin,
    SQLAlchemyBaseUserTable[UserIdType],
    ):
    ...
    @register(User, sqlalchemy_sessionmaker=db_helper.session_factory)
    class UserAdmin(SqlAlchemyModelAdmin):
    exclude = ("hash_password",)
    list_display = ("id", "email", "is_superuser", "is_active")
    list_display_links = ("id", "email")
    list_filter = ("id", "email", "is_superuser", "is_active")
    search_fields = ("email",)

    async def authenticate(self, email: str, password: str) -> UUID | int | None:
    sessionmaker = self.get_sessionmaker()
    async with sessionmaker() as session:
    query = select(self.model_cls).filter_by(email=email, password=password, is_superuser=True)
    result = await session.scalars(query)
    obj = result.first()
    if not obj:
    return None
    if not bcrypt.checkpw(password.encode(), obj.hash_password.encode()):
    return None
    return obj.id```

    Continue reading...

Compartilhe esta Página