本文整理汇总了Python中sqlalchemy.orm.properties.ColumnProperty方法的典型用法代码示例。如果您正苦于以下问题:Python properties.ColumnProperty方法的具体用法?Python properties.ColumnProperty怎么用?Python properties.ColumnProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm.properties
的用法示例。
在下文中一共展示了properties.ColumnProperty方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sqlalchemy_to_pydantic
# 需要导入模块: from sqlalchemy.orm import properties [as 别名]
# 或者: from sqlalchemy.orm.properties import ColumnProperty [as 别名]
def sqlalchemy_to_pydantic(
db_model: Type, *, exclude: Container[str] = []
) -> Type[BaseModel]:
"""
Mostly copied from https://github.com/tiangolo/pydantic-sqlalchemy
"""
mapper = inspect(db_model)
fields = {}
for attr in mapper.attrs:
if isinstance(attr, ColumnProperty):
if attr.columns:
column = attr.columns[0]
python_type = column.type.python_type
name = attr.key
if name in exclude:
continue
default = None
if column.default is None and not column.nullable:
default = ...
fields[name] = (python_type, default)
pydantic_model = create_model(
db_model.__name__, **fields # type: ignore
)
return pydantic_model
示例2: _get_column_metadata
# 需要导入模块: from sqlalchemy.orm import properties [as 别名]
# 或者: from sqlalchemy.orm.properties import ColumnProperty [as 别名]
def _get_column_metadata(prop):
name = prop.key
m2m = False
default = None
nullable = None
uselist = False
collection = None
proptype = type(prop)
if proptype is ColumnProperty:
coltype = type(prop.columns[0].type).__name__
try:
default = prop.columns[0].default
except AttributeError:
default = None
else:
if default is not None:
default = default.arg(None)
nullable = prop.columns[0].nullable
elif proptype is RelationshipProperty:
coltype = RelationshipProperty.__name__
m2m = prop.secondary is not None
nullable = prop.local_remote_pairs[0][0].nullable
uselist = prop.uselist
if prop.collection_class is not None:
collection = type(prop.collection_class()).__name__
else:
collection = "list"
else:
return None
return MetaDataTuple(coltype, str(name), default, m2m, nullable, uselist, collection)
示例3: walk
# 需要导入模块: from sqlalchemy.orm import properties [as 别名]
# 或者: from sqlalchemy.orm.properties import ColumnProperty [as 别名]
def walk(self):
for prop in self.iterate():
if isinstance(prop, (ColumnProperty, RelationshipProperty)):
if self.includes is None or prop.key in self.includes:
if self.excludes is None or prop.key not in self.excludes:
if prop not in self.history:
if not any(
c.foreign_keys for c in getattr(prop, "columns", Empty)
):
yield prop
示例4: from_csv
# 需要导入模块: from sqlalchemy.orm import properties [as 别名]
# 或者: from sqlalchemy.orm.properties import ColumnProperty [as 别名]
def from_csv(self, path):
"""
Load a snapshot of the ModelHub database from a set of CSVs in the given
directory.
"""
for model, table in [(self.Dataset, 'dataset'),
(self.Datarun, 'datarun'),
(self.Hyperpartition, 'hyperpartition'),
(self.Classifier, 'classifier')]:
df = pd.read_csv(os.path.join(path, '%ss.csv' % table))
# parse datetime columns. This is necessary because SQLAlchemy can't
# interpret strings as datetimes on its own.
# yes, this is the easiest way to do it
for c in inspect(model).attrs:
if not isinstance(c, ColumnProperty):
continue
col = c.columns[0]
if isinstance(col.type, DateTime):
df[c.key] = pd.to_datetime(df[c.key],
infer_datetime_format=True)
for _, r in df.iterrows():
# replace NaN and NaT with None
for k, v in list(r.iteritems()):
if pd.isnull(v):
r[k] = None
# insert the row into the database
create_func = getattr(self, 'create_%s' % table)
create_func(**r)
# ##########################################################################
# # Standard query methods ###############################################
# ##########################################################################
示例5: create_history
# 需要导入模块: from sqlalchemy.orm import properties [as 别名]
# 或者: from sqlalchemy.orm.properties import ColumnProperty [as 别名]
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)
示例6: get_fields
# 需要导入模块: from sqlalchemy.orm import properties [as 别名]
# 或者: from sqlalchemy.orm.properties import ColumnProperty [as 别名]
def get_fields(self):
ret = SortedDict()
mapper = class_mapper(self.cls.__class__)
r = self.context['request']
try:
# URI field for get pk field
pk_field = primary_key(self.cls.__class__)
ret['href'] = AlchemyUriField(source=pk_field,
path=r.build_absolute_uri(r.path),
read_only=True)
except KeyNotFoundException:
pass
# Get all the Column fields
for col_prop in mapper.iterate_properties:
if isinstance(col_prop, ColumnProperty):
field_nm = str(col_prop).split('.')[1]
field_cls = col_prop.columns[0].type.__class__
assert field_cls in self.field_mapping, \
"Field %s has not been mapped" % field_cls
ret[field_nm] = self.field_mapping[field_cls]()
# Get all the relationship fields
for rel_prop in mapper.iterate_properties:
if isinstance(rel_prop, RelationshipProperty):
field_nm = str(rel_prop).split('.')[1]
# many becomes same as uselist so that
# RelatedField can iterate over the queryset
kwargs = dict(
path=r.build_absolute_uri(r.path),
read_only=True
)
if rel_prop.uselist:
kwargs['many'] = True
ret[field_nm] = AlchemyRelatedField(**kwargs)
return ret
示例7: _build_properties
# 需要导入模块: from sqlalchemy.orm import properties [as 别名]
# 或者: from sqlalchemy.orm.properties import ColumnProperty [as 别名]
def _build_properties(
self, walker, root_schema, overrides, depth=None, history=None, toplevel=True
):
if depth is not None and depth <= 0:
return self.container_factory()
D = self.container_factory()
if history is None:
history = []
for prop in walker.walk():
for action, prop, opts in self.relation_decision.desicion(
walker, prop, toplevel
):
if action == RELATIONSHIP: # RelationshipProperty
history.append(prop)
subwalker = self.child_factory.child_walker(
prop, walker, history=history
)
suboverrides = self.child_factory.child_overrides(prop, overrides)
value = self.child_factory.child_schema(
prop,
self,
root_schema,
subwalker,
suboverrides,
depth=depth,
history=history,
)
self._add_property_with_reference(
walker, root_schema, D, prop, value
)
history.pop()
elif action == FOREIGNKEY: # ColumnProperty
for c in prop.columns:
sub = {}
if type(c.type) != VisitableType:
itype, sub["type"] = self.classifier[c.type]
self._add_restriction_if_found(sub, c, itype)
if c.doc:
sub["description"] = c.doc
if c.name in overrides:
overrides.overrides(sub)
if opts:
sub.update(opts)
D[c.name] = sub
else:
raise NotImplemented
D[prop.key] = sub
else: # immediate
D[prop.key] = action
return D