當前位置: 首頁>>代碼示例>>Python>>正文


Python properties.ColumnProperty方法代碼示例

本文整理匯總了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 
開發者ID:CTFd,項目名稱:CTFd,代碼行數:26,代碼來源:schemas.py

示例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) 
開發者ID:kdart,項目名稱:pycopia,代碼行數:32,代碼來源:models.py

示例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 
開發者ID:podhmo,項目名稱:alchemyjsonschema,代碼行數:12,代碼來源:__init__.py

示例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  ###############################################
    # ########################################################################## 
開發者ID:HDI-Project,項目名稱:ATM,代碼行數:37,代碼來源:database.py

示例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) 
開發者ID:alphagov,項目名稱:notifications-api,代碼行數:49,代碼來源:history_meta.py

示例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 
開發者ID:dealertrack,項目名稱:djangorest-alchemy,代碼行數:44,代碼來源:serializers.py

示例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 
開發者ID:podhmo,項目名稱:alchemyjsonschema,代碼行數:57,代碼來源:__init__.py


注:本文中的sqlalchemy.orm.properties.ColumnProperty方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。