本文整理汇总了Python中sqlalchemy.orm.class_mapper方法的典型用法代码示例。如果您正苦于以下问题:Python orm.class_mapper方法的具体用法?Python orm.class_mapper怎么用?Python orm.class_mapper使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm
的用法示例。
在下文中一共展示了orm.class_mapper方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def test_get(self):
class Foo(object):
def __init__(self, data=None):
self.data = data
class Bar(Foo):
pass
mapper(Foo, foo)
mapper(Bar, bar, inherits=Foo)
print(foo.join(bar).primary_key)
print(class_mapper(Bar).primary_key)
b = Bar("somedata")
sess = create_session()
sess.add(b)
sess.flush()
sess.expunge_all()
# test that "bar.bid" does not need to be referenced in a get
# (ticket 185)
assert sess.query(Bar).get(b.id).id == b.id
示例2: test_plain
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def test_plain(self):
# control case
class Base(object):
pass
class Sub(Base):
pass
mapper(Base, base)
mapper(Sub, subtable, inherits=Base)
# Sub gets a "base_id" property using the "base_id"
# column of both tables.
eq_(
class_mapper(Sub).get_property("base_id").columns,
[subtable.c.base_id, base.c.base_id],
)
示例3: test_attribute_error_raised_class_mapper
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def test_attribute_error_raised_class_mapper(self):
users = self.tables.users
addresses = self.tables.addresses
User = self.classes.User
Address = self.classes.Address
mapper(
User,
users,
properties={
"addresses": relationship(
Address,
primaryjoin=lambda: users.c.id == addresses.wrong.user_id,
)
},
)
mapper(Address, addresses)
assert_raises_message(
AttributeError,
"'Table' object has no attribute 'wrong'",
class_mapper,
Address,
)
示例4: test_key_error_raised_class_mapper
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def test_key_error_raised_class_mapper(self):
users = self.tables.users
addresses = self.tables.addresses
User = self.classes.User
Address = self.classes.Address
mapper(
User,
users,
properties={
"addresses": relationship(
Address,
primaryjoin=lambda: users.c.id
== addresses.__dict__["wrong"].user_id,
)
},
)
mapper(Address, addresses)
assert_raises_message(KeyError, "wrong", class_mapper, Address)
示例5: test_partially_mapped_inheritance
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def test_partially_mapped_inheritance(self):
class A(object):
pass
class B(A):
pass
class C(B):
def __init__(self, x):
pass
mapper(A, self.fixture())
# B is not mapped in the current implementation
assert_raises(sa.orm.exc.UnmappedClassError, class_mapper, B)
# C is not mapped in the current implementation
assert_raises(sa.orm.exc.UnmappedClassError, class_mapper, C)
示例6: _fixture
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def _fixture(self):
A, B = self.classes.A, self.classes.B
session = create_session()
uowcommit = self._get_test_uow(session)
a_mapper = class_mapper(A)
b_mapper = class_mapper(B)
self.a1 = a1 = A()
self.b1 = b1 = B()
uowcommit = self._get_test_uow(session)
return (
uowcommit,
attributes.instance_state(a1),
attributes.instance_state(b1),
a_mapper,
b_mapper,
)
示例7: test_mapper_args_declared_attr
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def test_mapper_args_declared_attr(self):
class ComputedMapperArgs:
@declared_attr
def __mapper_args__(cls):
if cls.__name__ == "Person":
return {"polymorphic_on": cls.discriminator}
else:
return {"polymorphic_identity": cls.__name__}
class Person(Base, ComputedMapperArgs):
__tablename__ = "people"
id = Column(Integer, primary_key=True)
discriminator = Column("type", String(50))
class Engineer(Person):
pass
configure_mappers()
assert class_mapper(Person).polymorphic_on is Person.__table__.c.type
eq_(class_mapper(Engineer).polymorphic_identity, "Engineer")
示例8: test_mapper_args_declared_attr_two
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def test_mapper_args_declared_attr_two(self):
# same as test_mapper_args_declared_attr, but we repeat
# ComputedMapperArgs on both classes for no apparent reason.
class ComputedMapperArgs:
@declared_attr
def __mapper_args__(cls):
if cls.__name__ == "Person":
return {"polymorphic_on": cls.discriminator}
else:
return {"polymorphic_identity": cls.__name__}
class Person(Base, ComputedMapperArgs):
__tablename__ = "people"
id = Column(Integer, primary_key=True)
discriminator = Column("type", String(50))
class Engineer(Person, ComputedMapperArgs):
pass
configure_mappers()
assert class_mapper(Person).polymorphic_on is Person.__table__.c.type
eq_(class_mapper(Engineer).polymorphic_identity, "Engineer")
示例9: test_single_table_no_propagation
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def test_single_table_no_propagation(self):
class IdColumn:
id = Column(Integer, primary_key=True)
class Generic(Base, IdColumn):
__tablename__ = "base"
discriminator = Column("type", String(50))
__mapper_args__ = dict(polymorphic_on=discriminator)
value = Column(Integer())
class Specific(Generic):
__mapper_args__ = dict(polymorphic_identity="specific")
assert Specific.__table__ is Generic.__table__
eq_(list(Generic.__table__.c.keys()), ["id", "type", "value"])
assert (
class_mapper(Specific).polymorphic_on is Generic.__table__.c.type
)
eq_(class_mapper(Specific).polymorphic_identity, "specific")
示例10: test_we_must_only_copy_column_mapper_args
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def test_we_must_only_copy_column_mapper_args(self):
class Person(Base):
__tablename__ = "people"
id = Column(Integer, primary_key=True)
a = Column(Integer)
b = Column(Integer)
c = Column(Integer)
d = Column(Integer)
discriminator = Column("type", String(50))
__mapper_args__ = {
"polymorphic_on": discriminator,
"polymorphic_identity": "person",
"version_id_col": "a",
"column_prefix": "bar",
"include_properties": ["id", "a", "b"],
}
assert class_mapper(Person).version_id_col == "a"
assert class_mapper(Person).include_properties == set(["id", "a", "b"])
示例11: test_subclass_mixin
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def test_subclass_mixin(self):
class Person(Base, fixtures.ComparableEntity):
__tablename__ = "people"
id = Column("id", Integer, primary_key=True)
name = Column("name", String(50))
discriminator = Column("type", String(50))
__mapper_args__ = {"polymorphic_on": discriminator}
class MyMixin(object):
pass
class Engineer(MyMixin, Person):
__tablename__ = "engineers"
__mapper_args__ = {"polymorphic_identity": "engineer"}
id = Column(
"id", Integer, ForeignKey("people.id"), primary_key=True
)
primary_language = Column("primary_language", String(50))
assert class_mapper(Engineer).inherits is class_mapper(Person)
示例12: to_dict
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def to_dict(self, obj):
d = {}
for col in class_mapper(obj.__class__).mapped_table.c:
d[col.name] = getattr(obj, col.name)
if d[col.name] and col.name.endswith('time'):
d[col.name] = getattr(obj, col.name).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
try:
d['tags'] = [t.tag for t in obj.tags]
except AttributeError:
pass
try:
d['message'] = [b64encode(m.message) for m in obj.messages]
except AttributeError:
pass
return d
示例13: model_to_dict
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def model_to_dict(model):
model_dict = {}
for key, column in class_mapper(model.__class__).c.items():
model_dict[column.name] = getattr(model, key, None)
return model_dict
示例14: __get__
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def __get__(self, obj, type):
try:
mapper = orm.class_mapper(type)
if mapper:
return type.query_class(mapper, session=self.sa.session())
except UnmappedClassError:
return None
示例15: as_dict
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import class_mapper [as 别名]
def as_dict(self):
_dict = OrderedDict()
table = orm.class_mapper(self.__class__).mapped_table
for col in table.c:
val = getattr(self, col.name)
if isinstance(val, datetime.date):
val = str(val)
if isinstance(val, datetime.datetime):
val = val.isoformat()
_dict[col.name] = val
return _dict