1. You're serializing the SqlAlchemy object, not the data.

In order to get the underlying data from an object, I PERSONALLY use a 
mixin class for my declarative base that offers `columns_as_dict`:


def columns_as_dict(self):
return dict((col.name, getattr(self, col.name)) for col in 
sqlalchemy_orm.class_mapper(self.__class__).mapped_table.c)

That returns the columns as a dict, however it does not handle the 
relationships -- which is separate.

2. You can't just dump sqlalchemy object data into a json encoder.  The 
reason is the json package doesn't natively handle Datetime or many other 
sqlalchemy formats.  You'll have to write a custom json serializer.

if you're writing a custom json serializer, many people prefer to handle a 
condition where the object is a SqlAlchemy object and then recursively 
iterate through that.  

3. Some people like to explicitly map out which object columns are in their 
JSON format.  Using a mixin, something like:

    class JsonExport(object):
          _json_columns = None

        def column_data(self):
           if self._json_columns is None:
              return {}
           return {k: getattr(self, k) for k in self._json_columns}

    class Student(JsonExport):
      _json_columns = ['name', 'fillname', 'password', 'id', ]


you'll still need a custom json encoder though.

-- 
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.

Reply via email to