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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.