exclude password column in query sqlalchemy

Question:

I need to know how to query by excluding password column.
is it possible?

query = await db.execute(
        select(User)
        .filter(User.id.in_(users.user_ids)).where(User.is_deleted== False))
    list_user = query.scalars().unique().all()
Asked By: Masy

||

Answers:

You can use the "Deferred Column Loading" feature. For example, if you want the password column to be loaded only upon direct access, instead of when the entity is queried, you can define it using the deferred function in your model:

from sqlalchemy.orm import deferred
from sqlalchemy import String, Column, ...


class User(Base):
    __tablename__ = "user"

    username = Column(String(200), nullable=False)
    ...
    password = deferred(Column(String(2000)))
    ...
Answered By: jurel
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.