Python sqlalchemy.orm.properties.ColumnProperty() Examples
The following are 7
code examples of sqlalchemy.orm.properties.ColumnProperty().
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.properties
, or try the search function
.
Example #1
Source File: schemas.py From CTFd with Apache License 2.0 | 6 votes |
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
Example #2
Source File: models.py From pycopia with Apache License 2.0 | 5 votes |
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)
Example #3
Source File: __init__.py From alchemyjsonschema with MIT License | 5 votes |
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
Example #4
Source File: database.py From ATM with MIT License | 5 votes |
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 ############################################### # ##########################################################################
Example #5
Source File: history_meta.py From notifications-api with MIT License | 4 votes |
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 #6
Source File: serializers.py From djangorest-alchemy with MIT License | 4 votes |
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
Example #7
Source File: __init__.py From alchemyjsonschema with MIT License | 4 votes |
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