Home > Back-end >  exclude password column in query sqlalchemy
exclude password column in query sqlalchemy

Time:10-12

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()

CodePudding user response:

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)))
    ...
  • Related