- class LanguageMapping(MappedCollection):
- """Baby class that converts a language identifier key into an actual
- language object, allowing for `foo.bars['en'] = Translations(...)`.
-
- Needed for per-column association proxies to function as setters.
- """
- @collection.internally_instrumented
- def __setitem__(self, key, value, _sa_initiator=None):
- if key in self:
- raise NotImplementedError("Can't replace the whole row, sorry!")
-
- # Only do this nonsense if the value is a dangling object; if it's
- # in the db it already has its language_id
- if not object_session(value):
- # This took quite some source-diving to find, but it oughta be
- # the object that actually owns this collection.
- obj = collection_adapter(self).owner_state.obj()
- session = object_session(obj)
- value.language = session.query(_language_class) \
- .filter_by(identifier=key).one()
-
- super(LanguageMapping, self).__setitem__(key, value, _sa_initiator)
-