Make `flatten` work for nested models and collections