[Python] method of iterating over sqlalchemy model's defined columns?

Discussão em 'Python' iniciado por Stack, Setembro 7, 2025.

  1. Stack

    Stack Membro Participativo

    I've been trying to figure out how to iterate over the list of columns defined in a SQLAlchemy model. I want it for writing some serialization and copy methods to a couple of models. I can't just iterate over the obj.__dict__ since it contains a lot of SA specific items.

    Anyone know of a way to just get the id and desc names from the following?

    class JobStatus(Base):
    __tablename__ = 'jobstatus'

    id = Column(Integer, primary_key=True)
    desc = Column(Unicode(20))


    In this small case I could easily create a:

    def logme(self):
    return {'id': self.id, 'desc': self.desc}


    but I'd prefer something that auto-generates the dict (for larger objects).

    Continue reading...

Compartilhe esta Página