Take the following code:
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Integer, String
engine = create_engine('postgresql://postgres:password@localhost:5432/db', echo=True, echo_pool='debug')
Base = declarative_base()
class Item(Base):
__tablename__ = 'items'
id = Column(Integer, primary_key=True)
name = Column(String)
def __repr__(self):
return "<Item(id=%s, name='%s')>" % (self.id, self.name)
item = Item(name="computer")
Is there a way to get a python dict of the Item
with all its fields? For example, I would like to get the following returned:
item.to_dict()
{"id": None, "name": "computer"}
Or do I have to write my own method to do that?