Python sqlalchemy.orm.attributes.QueryableAttribute() Examples
The following are 5
code examples of sqlalchemy.orm.attributes.QueryableAttribute().
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.attributes
, or try the search function
.
Example #1
Source File: utils.py From sqlalchemy-json-api with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_descriptor_columns(model, descriptor): if isinstance(descriptor, InstrumentedAttribute): return descriptor.property.columns elif isinstance(descriptor, sa.orm.ColumnProperty): return descriptor.columns elif isinstance(descriptor, sa.Column): return [descriptor] elif isinstance(descriptor, sa.sql.expression.ClauseElement): return [] elif isinstance(descriptor, sa.ext.hybrid.hybrid_property): expr = descriptor.expr(model) try: return get_descriptor_columns(model, expr) except TypeError: return [] elif ( isinstance(descriptor, QueryableAttribute) and hasattr(descriptor, 'original_property') ): return get_descriptor_columns(model, descriptor.property) raise TypeError( 'Given descriptor is not of type InstrumentedAttribute, ' 'ColumnProperty or Column.' )
Example #2
Source File: base.py From cloud-inquisitor with Apache License 2.0 | 5 votes |
def to_json(self): """Exports the object to a JSON friendly dict Returns: Dict representation of object type """ output = { '__type': self.__class__.__name__ } cls_attribs = self.__class__.__dict__ for attrName in cls_attribs: attr = getattr(self.__class__, attrName) value = getattr(self, attrName) value_class = type(value) if issubclass(type(attr), QueryableAttribute): # List of Model, BaseModelMixin objects (one-to-many relationship) if issubclass(value_class, InstrumentedList): output[to_camelcase(attrName)] = [x.to_json() for x in value] # Model, BaseModelMixin object (one-to-one relationship) elif issubclass(value_class, Model): output[to_camelcase(attrName)] = value.to_json() # Datetime object elif isinstance(value, datetime): output[to_camelcase(attrName)] = isoformat(value) elif isinstance(value, enum.Enum): output[to_camelcase(attrName)] = value.name # Any primitive type else: output[to_camelcase(attrName)] = value return output
Example #3
Source File: helpers.py From planespotter with MIT License | 5 votes |
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]
Example #4
Source File: alchemy.py From flask-rest-jsonapi with MIT License | 5 votes |
def apply_nested_fields(self, data, obj): nested_fields_to_apply = [] nested_fields = get_nested_fields(self.resource.schema, model_field=True) for key, value in data.items(): if key in nested_fields: nested_field_inspection = inspect(getattr(obj.__class__, key)) if not isinstance(nested_field_inspection, QueryableAttribute): raise InvalidType("Unrecognized nested field type: not a queryable attribute.") if isinstance(nested_field_inspection.property, RelationshipProperty): nested_model = getattr(obj.__class__, key).property.mapper.class_ if isinstance(value, list): nested_objects = [] for identifier in value: nested_object = nested_model(**identifier) nested_objects.append(nested_object) nested_fields_to_apply.append({'field': key, 'value': nested_objects}) else: nested_fields_to_apply.append({'field': key, 'value': nested_model(**value)}) elif isinstance(nested_field_inspection.property, ColumnProperty): nested_fields_to_apply.append({'field': key, 'value': value}) else: raise InvalidType("Unrecognized nested field type: not a RelationshipProperty or ColumnProperty.") for nested_field in nested_fields_to_apply: setattr(obj, nested_field['field'], nested_field['value'])
Example #5
Source File: columns.py From sqlakeyset with The Unlicense | 4 votes |
def derive_order_key(ocol, desc, index): """Attempt to derive the value of `ocol` from a query column. :param ocol: The :class:`OC` to look up. :param desc: Either a column description as in :attr:`sqlalchemy.orm.query.Query.column_descriptions`, or a :class:`sqlalchemy.sql.expression.ColumnElement`. :returns: Either a :class:`MappedOrderColumn` or `None`.""" if isinstance(desc, ColumnElement): if desc.compare(ocol.comparable_value): return DirectColumn(ocol, index) else: return None entity = desc['entity'] expr = desc['expr'] if isinstance(expr, Bundle): for key, col in expr.columns.items(): if strip_labels(col).compare(ocol.comparable_value): return AttributeColumn(ocol, index, key) try: is_a_table = bool(entity == expr) except (sqlalchemy.exc.ArgumentError, TypeError): is_a_table = False if isinstance(expr, Mapper) and expr.class_ == entity: is_a_table = True if is_a_table: # is a table mapper = class_mapper(desc['type']) try: prop = mapper.get_property_by_column(ocol.element) return AttributeColumn(ocol, index, prop.key) except sqlalchemy.orm.exc.UnmappedColumnError: pass # is an attribute if isinstance(expr, QueryableAttribute): mapper = expr.parent tname = mapper.local_table.description if ocol.table_name == tname and ocol.name == expr.name: return DirectColumn(ocol, index) # is an attribute with label try: if ocol.quoted_full_name == OC(expr).full_name: return DirectColumn(ocol, index) except sqlalchemy.exc.ArgumentError: pass