Skip to content Skip to sidebar Skip to footer

Print An Sqlalchemy Row

All I'd like to do is print a single row of an sqlalchemy table row. Say I have: from sqlalchemy import Column, Integer, String from sqlalchemy.ext.declarative import declarative_b

Solution 1:

To be clear - you want a general method to print "col: value" without hardcoding the column names? I do not use SQLAlchemy much, but a __str__ method like this should work:

def __str__(self):
    output = ''
    for c in self.__table__.columns:
        output += '{}: {}\n'.format(c.name, getattr(self, c.name))
    return output

You can then put that method in a mixin class to use elsewhere in your models.


Post a Comment for "Print An Sqlalchemy Row"