Create ORM object from dict and add to session
<p>Let's say I have a <code>User</code> model with attributes <code>id</code>, <code>name</code>, <code>email</code> and a relationship <code>languages</code>.
Is it possible to create a <code>User</code> instance from existing data that behaves like I would have queried it with <code>dbsession.query(User).get(42)</code>?
What I mean in particular is that I want that an access to <code>user.languages</code> creates a subquery and populates the attribute.</p>
<p>Here a code example:</p>
<p>I have a class <code>User</code>:</p>
<pre><code>class User(Base):
    id = Column(Integer, primary_key=True)
    name = Column(String(64))
    email = Column(String(64))

    languages = relationship('Language', secondary='user_languages')
</code></pre>
<p>I already have a lot of users stored in my DB.
And I know that I have, for example, this user in my DB:</p>
<pre><code>user_dict = {
    'id': 23,
    'name': 'foo',
    'email': 'foo@bar',
}
</code></pre>
<p>So I have all the attributes but the relations.
Now I want to make a sqlalchemy <code>User</code> instance
and kind of register it in sqlalchemy's system
so I can get the <code>languages</code> if needed.</p>
<pre><code>user = User(**user_dict)

# Now I can access the id, name email attributes
assert user.id == 23

# but since sqlalchemy thinks it's a new model it doesn't
# lazy load any relationships
assert len(user.languages) == 0
# I want here that the languages for the user with id 23 appear

# So I want that `user` is the same as when I would have done
user_from_db =  DBSession.query(User).get(23)
assert user == user_from_db
</code></pre>
<p>The use-case is that I have a big model with lots of complex
relationships but 90% of the time I don't need the data from those.
So I only want to cache the direct attributes plus what else I need
and then load those from the cache like above and be able to
use the sqlalchemy model like I would have queried it from the db.</p>