我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用sqlalchemy.orm.ColumnProperty()。
def primary_key_names(model): """Returns all the primary keys for a model.""" return [key for key, field in inspect.getmembers(model) if isinstance(field, QueryableAttribute) and isinstance(field.property, ColumnProperty) and field.property.columns[0].primary_key]
def post_configure_attribute_07(self, class_, key, inst): """Add validators for any attributes that can be validated.""" # SQLAlchemy >= 0.7 (using events) prop = inst.prop # Only interested in simple columns, not relations if isinstance(prop, ColumnProperty) and len(prop.columns) == 1: col = prop.columns[0] # if we have string column with a length, install a length validator if isinstance(col.type, String) and col.type.length: event.listen(inst, 'set', LengthValidator(col.name, col.type.length).set, retval=True)
def post_configure_attribute_pre_07(self, class_, key, inst): """Add validators for any attributes that can be validated.""" # SQLAlchemy < 0.7 (using extensions) prop = inst.prop # Only interested in simple columns, not relations if isinstance(prop, ColumnProperty) and len(prop.columns) == 1: col = prop.columns[0] # if we have string column with a length, install a length validator if isinstance(col.type, String) and col.type.length: inst.impl.extensions.insert(0, LengthValidator(col.name, col.type.length))
def _sav_column_names(self): return [p.key for p in self.__mapper__.iterate_properties if isinstance(p, saorm.ColumnProperty)]
def _column_to_property(self, column): if isinstance(column, hybrid_property): attr_key = column.__name__ for key, attr in self.parent.all_orm_descriptors.items(): if key == attr_key: return attr else: for key, attr in self.parent.attrs.items(): if isinstance(attr, ColumnProperty): if attr.columns[0].name == column.name: return attr