本文整理汇总了Python中sqlalchemy.ext.declarative.declared_attr方法的典型用法代码示例。如果您正苦于以下问题:Python declarative.declared_attr方法的具体用法?Python declarative.declared_attr怎么用?Python declarative.declared_attr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.ext.declarative
的用法示例。
在下文中一共展示了declarative.declared_attr方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_column
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declared_attr [as 别名]
def create_column(self, cname, remote_type, foreign_key, properties):
def wrapper(cls):
return SA_Column(
cname.attribute_name,
remote_type,
nullable=self.nullable,
unique=self.unique,
index=self.index,
primary_key=self.primary_key,
info=dict(label=self.label, foreign_key=foreign_key))
properties[(anyblok_column_prefix +
cname.attribute_name)] = declared_attr(wrapper)
properties['loaded_columns'].append(cname.attribute_name)
properties['hybrid_property_columns'].append(cname.attribute_name)
properties[cname.attribute_name] = hybrid_property(
self.wrap_getter_column(cname.attribute_name),
super(Many2One, self).wrap_setter_column(cname.attribute_name),
expr=self.wrap_expr_column(cname.attribute_name))
示例2: test_mapper_args_deferred
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declared_attr [as 别名]
def test_mapper_args_deferred(self):
"""test that __mapper_args__ is not called until *after*
table reflection"""
class User(decl.DeferredReflection, fixtures.ComparableEntity, Base):
__tablename__ = "users"
@decl.declared_attr
def __mapper_args__(cls):
return {"primary_key": cls.__table__.c.id}
decl.DeferredReflection.prepare(testing.db)
sess = Session()
sess.add_all(
[User(name="G"), User(name="Q"), User(name="A"), User(name="C")]
)
sess.commit()
eq_(
sess.query(User).order_by(User.name).all(),
[User(name="A"), User(name="C"), User(name="G"), User(name="Q")],
)
示例3: insert_in_bases_table_args
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declared_attr [as 别名]
def insert_in_bases_table_args(self, new_base, transformation_properties):
"""Add table __table_args__ in new_base
:param new_base: the base to be put on front of all bases
:param transformation_properties: the properties of the model
"""
if (
transformation_properties['table_args'] and
transformation_properties['table_kwargs']
):
def __table_args__(cls_):
res = cls_.define_table_args() + (cls_.define_table_kwargs(),)
return res
new_base.__table_args__ = declared_attr(__table_args__)
elif transformation_properties['table_args']:
def __table_args__(cls_):
return cls_.define_table_args()
new_base.__table_args__ = declared_attr(__table_args__)
elif transformation_properties['table_kwargs']:
def __table_args__(cls_):
return cls_.define_table_kwargs()
new_base.__table_args__ = declared_attr(__table_args__)
示例4: insert_in_bases_mapper_args
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declared_attr [as 别名]
def insert_in_bases_mapper_args(self, new_base, transformation_properties):
"""Add table __mapper_args__ in new_base
:param new_base: the base to be put on front of all bases
:param transformation_properties: the properties of the model
"""
if transformation_properties['mapper_args']:
def __mapper_args__(cls_):
return cls_.define_mapper_args()
new_base.__mapper_args__ = declared_attr(__mapper_args__)
示例5: should_set_tablename
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declared_attr [as 别名]
def should_set_tablename(cls):
"""Determine whether ``__tablename__`` should be automatically generated
for a model.
* If no class in the MRO sets a name, one should be generated.
* If a declared attr is found, it should be used instead.
* If a name is found, it should be used if the class is a mixin, otherwise
one should be generated.
* Abstract models should not have one generated.
Later, :meth:`._BoundDeclarativeMeta.__table_cls__` will determine if the
model looks like single or joined-table inheritance. If no primary key is
found, the name will be unset.
"""
if (
cls.__dict__.get('__abstract__', False)
or not any(isinstance(b, DeclarativeMeta) for b in cls.__mro__[1:])
):
return False
for base in cls.__mro__:
if '__tablename__' not in base.__dict__:
continue
if isinstance(base.__dict__['__tablename__'], declared_attr):
return False
return not (
base is cls
or base.__dict__.get('__abstract__', False)
or not isinstance(base, DeclarativeMeta)
)
return True
示例6: declare_field
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declared_attr [as 别名]
def declare_field(cls, registry, name, field, namespace, properties,
transformation_properties):
""" Declare the field/column/relationship to put in the properties
of the model
:param registry: the current registry
:param name: name of the field / column or relationship
:param field: the declaration field / column or relationship
:param namespace: the namespace of the model
:param properties: the properties of the model
"""
if name in properties['loaded_columns']:
return
if field.must_be_copied_before_declaration():
field = deepcopy(field)
attr_name = name
if field.use_hybrid_property:
attr_name = anyblok_column_prefix + name
if field.must_be_declared_as_attr():
# All the declaration are seen as mixin for sqlalchemy
# some of them need de be defered for the initialisation
# cause of the mixin as relation ship and column with foreign key
def wrapper(cls):
return field.get_sqlalchemy_mapping(
registry, namespace, name, properties)
properties[attr_name] = declared_attr(wrapper)
properties[attr_name].anyblok_field = field
else:
properties[attr_name] = field.get_sqlalchemy_mapping(
registry, namespace, name, properties)
if field.use_hybrid_property:
properties[name] = field.get_property(
registry, namespace, name, properties)
properties['hybrid_property_columns'].append(name)
registry.call_plugins('declare_field', name, field, namespace,
properties, transformation_properties)
properties['loaded_columns'].append(name)
field.update_properties(registry, namespace, name, properties)