Hey Iain,
I basically use two patterns when it comes to handling the impedance mismatch between my persistence layer (DB with SQLAlchemy), domain models, and view models (JSON objects for Angular.js):
1.) This first pattern I use consists of maintaining two separate models for the domain and persistence layer. I personally prefer to use SQLAlchemy's declarative approach, but many times my domain models do not associate directly with all the fields in the persistence models. In these situations I rely heavily on the Repository Pattern (
https://msdn.microsoft.com/en-us/library/ff649690.aspx) for dealing with the conversion of domain objects to the persistence objects. Unfortunately, this results in a quite a bit of redundant code since the domain models will contain most of the same fields and structures as the persistence models. However, I've found that the benefit of maintaining a good separation of concerns between the persistence layer and my domain models provides major benefits.
2.) The second method that I use is adding to_json and from_json static functions to my domain models or SQLAlchemy model objects (depending on how rich your domain model structure is and how big your project is). When I validate incoming JSON from the web (or any other service) I use Colander to validate the structure and turn the object to a dictionary. I will then use the from_json function to convert the object to the appropriate the persistence model or domain model. On the other hand, when I want to convert my domain object to JSON, I use the to_json function. This gives me the greatest control over the fields that I want to expose to JSON without needing an additional libraries or needing to define custom serializers/deserializers.
Overall, I've found that's it's best to maintain distinct models for my persistence layer, domain model and view model even if it tends to be redundant. When I need to convert between two different object types, e.g. model view (JSON) to domain model, I simply add the static function to the object I need to convert to.
These patterns have really helped me deal with a number of Pyramid applications that have grown quite large over the past few years.
-Vincent