Re: [sqlalchemy] turning only loaded columns into a dict
On Wednesday, May 2, 2018 at 5:53:56 PM UTC-4, Mike Bayer wrote: > > if you only care about things that are loaded, like before, look in > inspect(instance).dict , that's what's loaded > Thanks. I'll migrate my proof-of-concept to use `inspect`. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] turning only loaded columns into a dict
if you only care about things that are loaded, like before, look in inspect(instance).dict , that's what's loaded On Wed, May 2, 2018 at 4:24 PM, Jonathan Vanascowrote: > I have a mixin that helps convert object to JSON using a `columns_as_dict` > method. > > it looks like this: > > from sqlalchemy.orm import class_mapper as sa_class_mapper > > class Mixin(object): > def columns_as_dict(self): > _cls = self.__class__ > return dict((col.name, getattr(self, col.name)) for col in > sa_class_mapper(_cls).mapped_table.c) > > > I pinpointed a performance issue where the db was getting hit when > `load_only` was used on the objects. > > The simplest fix I could think of, is fetching column values from the > object's dict instead of via getattr . Is there a more appropriate way? > > from sqlalchemy.orm import class_mapper as sa_class_mapper > > class Mixin(object): > def columns_as_dict(self): > _cls = self.__class__ > return {col.name: self.__dict__[col.name] > for col in sa_class_mapper(_cls).mapped_table.c > if col.name in self.__dict__ > } > > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] turning only loaded columns into a dict
I have a mixin that helps convert object to JSON using a `columns_as_dict` method. it looks like this: from sqlalchemy.orm import class_mapper as sa_class_mapper class Mixin(object): def columns_as_dict(self): _cls = self.__class__ return dict((col.name, getattr(self, col.name)) for col in sa_class_mapper(_cls).mapped_table.c) I pinpointed a performance issue where the db was getting hit when `load_only` was used on the objects. The simplest fix I could think of, is fetching column values from the object's dict instead of via getattr . Is there a more appropriate way? from sqlalchemy.orm import class_mapper as sa_class_mapper class Mixin(object): def columns_as_dict(self): _cls = self.__class__ return {col.name: self.__dict__[col.name] for col in sa_class_mapper(_cls).mapped_table.c if col.name in self.__dict__ } -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.