Python sqlalchemy.orm.object_mapper() Examples

The following are 13 code examples of sqlalchemy.orm.object_mapper(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.orm , or try the search function .
Example #1
Source File: models.py    From rucio with Apache License 2.0 5 votes vote down vote up
def __iter__(self):
        self._i = iter(object_mapper(self).columns)
        return self 
Example #2
Source File: template.py    From snippet with MIT License 5 votes vote down vote up
def __iter__(self):
        return ModelIterator(self, iter(dict(object_mapper(self).columns).keys())) 
Example #3
Source File: sqlalchemy-orm-model.py    From snippet with MIT License 5 votes vote down vote up
def __iter__(self):
        columns = list(dict(object_mapper(self).columns).keys())
        return ModelIterator(self, iter(columns)) 
Example #4
Source File: check_music.py    From snippet with MIT License 5 votes vote down vote up
def __iter__(self):
        columns = list(dict(object_mapper(self).columns).keys())
        return ModelIterator(self, iter(columns)) 
Example #5
Source File: models.py    From sgx-kms with Apache License 2.0 5 votes vote down vote up
def __iter__(self):
        self._i = iter(orm.object_mapper(self).sa.Columns)
        return self 
Example #6
Source File: model_base.py    From tacker with Apache License 2.0 5 votes vote down vote up
def __iter__(self):
        self._i = iter(orm.object_mapper(self).columns)
        return self 
Example #7
Source File: models.py    From barbican with Apache License 2.0 5 votes vote down vote up
def __iter__(self):
        self._i = iter(orm.object_mapper(self).sa.Columns)
        return self 
Example #8
Source File: models.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def __iter__(self):
        columns = list(dict(object_mapper(self).columns).keys())
        # NOTE(russellb): Allow models to specify other keys that can be looked
        # up, beyond the actual db columns.  An example would be the 'name'
        # property for an Instance.
        columns.extend(self._extra_keys)

        return ModelIterator(self, iter(columns)) 
Example #9
Source File: test_cascade.py    From sqlalchemy with MIT License 5 votes vote down vote up
def _assert_not_orphan(self, c1):
        mapper = object_mapper(c1)
        state = instance_state(c1)
        assert not mapper._is_orphan(state) 
Example #10
Source File: test_cascade.py    From sqlalchemy with MIT License 5 votes vote down vote up
def _assert_is_orphan(self, c1):
        mapper = object_mapper(c1)
        state = instance_state(c1)
        assert mapper._is_orphan(state) 
Example #11
Source File: model_base.py    From neutron-lib with Apache License 2.0 5 votes vote down vote up
def __iter__(self):
        self._i = iter(orm.object_mapper(self).columns)
        return self 
Example #12
Source File: history_meta.py    From notifications-api with MIT License 4 votes vote down vote up
def create_history(obj, history_cls=None):
    if not history_cls:
        history_mapper = obj.__history_mapper__
        history_cls = history_mapper.class_

    obj_mapper = object_mapper(obj)

    obj_state = attributes.instance_state(obj)
    data = {}
    for prop in obj_mapper.iterate_properties:

        # expired object attributes and also deferred cols might not
        # be in the dict.  force it them load no matter what by using getattr().
        if prop.key not in obj_state.dict:
            getattr(obj, prop.key)

        # if prop is a normal col just set it on history model
        if isinstance(prop, ColumnProperty):
            if not data.get(prop.key):
                data[prop.key] = getattr(obj, prop.key)

        # if the prop is a relationship property and there is a
        # corresponding prop on hist object then set the
        # relevant "_id" prop to the id of the current object.prop.id.
        # This is so foreign keys get set on history when
        # the source object is new and therefore property foo_id does
        # not yet have a value before insert

        elif isinstance(prop, RelationshipProperty):
            if hasattr(history_cls, prop.key + '_id'):
                foreign_obj = getattr(obj, prop.key)
                # if it's a nullable relationship, foreign_obj will be None, and we actually want to record that
                data[prop.key + '_id'] = getattr(foreign_obj, 'id', None)

    if not obj.version:
        obj.version = 1
        obj.created_at = datetime.datetime.utcnow()
    else:
        obj.version += 1
        now = datetime.datetime.utcnow()
        obj.updated_at = now
        data['updated_at'] = now

    data['version'] = obj.version
    data['created_at'] = obj.created_at

    return history_cls(**data) 
Example #13
Source File: test_basic.py    From sqlalchemy with MIT License 4 votes vote down vote up
def _roundtrip(
        self, set_event=True, parent_ident="parent", child_ident="child"
    ):
        Parent, Child = self.classes.Parent, self.classes.Child

        # locate the "polymorphic_on" ColumnProperty.   This isn't
        # "officially" stored at the moment so do some heuristics to find it.
        parent_mapper = inspect(Parent)
        for prop in parent_mapper.column_attrs:
            if not prop.instrument:
                break
        else:
            prop = parent_mapper._columntoproperty[
                parent_mapper.polymorphic_on
            ]

        # then make sure the column we will query on matches.
        is_(parent_mapper.polymorphic_on, prop.columns[0])

        if set_event:

            @event.listens_for(Parent, "init", propagate=True)
            def set_identity(instance, *arg, **kw):
                ident = object_mapper(instance).polymorphic_identity
                if ident == "parent":
                    instance.x = parent_ident
                elif ident == "child":
                    instance.x = child_ident
                else:
                    assert False, "Got unexpected identity %r" % ident

        s = Session(testing.db)
        s.add_all([Parent(q="p1"), Child(q="c1", y="c1"), Parent(q="p2")])
        s.commit()
        s.close()

        eq_(
            [type(t) for t in s.query(Parent).order_by(Parent.id)],
            [Parent, Child, Parent],
        )

        eq_([type(t) for t in s.query(Child).all()], [Child])