twilio/flask-restful — pull request #32 by yaniv-aknin (2012-12-18 15:38:06+00:00)
Title: Add custom marshalling hook for to_marshallable_type

In issue [#20](https://github.com/twilio/flask-restful/issues/20) I described bad interaction between flask-restful and SQLAlchemy related objects. When serializing, flask-restful's `to_marshallable_type()` relies on an instance's __dict__ to contain the necessary state for serialization, which isn't always enough and definitely isn't enough for freshly fetched SQLAlchemy instances with related attributes, as these are populated lazily in the instance by proxies in the object's class.

After further discussing the matter on SQLAlchemy's mailing [list](https://groups.google.com/d/topic/sqlalchemy/7XVtKUYcjok/discussion), I believe the only correct way to handle this is to make marshalling optionally customizable, which this patch proposes by adding a `__marshallable__` method to an instance (I'd go with any other name, I dislike the name myself).

I'd like to note here that alternative solutions could instead (or in addition) add introspection logic to `to_marshallable_type()`. For example, `return dict((key, getattr(obj, key)) for key in dir(obj))` will solve the particular poor interaction with SQLAlchemy. However, I believe any such added introspection power would add complexity while still not being 100% correct, a poor trade. Hence, I propose this patch for merge as is.