Models¶
-
class
schematics.models.
FieldDescriptor
(name)¶ The FieldDescriptor serves as a wrapper for Types that converts them into fields.
A field is then the merger of a Type and it’s Model.
-
class
schematics.models.
Model
(raw_data=None, deserialize_mapping=None, strict=True)¶ Enclosure for fields and validation. Same pattern deployed by Django models, SQLAlchemy declarative extension and other developer friendly libraries.
Initial field values can be passed in as keyword arguments to
__init__
to initialize the object with. Can raiseConversionError
if it is not possible to convert the raw data into richer Python constructs.-
classmethod
allow_none
(field)¶ Inspects a field and class for
serialize_when_none
setting.The setting defaults to the value of the class. A field can override the class setting with it’s own
serialize_when_none
setting.
-
atoms
()¶ Iterator for the atomic components of a model definition and relevant data that creates a threeple of the field’s name, the instance of it’s type, and it’s value.
-
convert
(raw_data, **kw)¶ Converts the raw data into richer Python constructs according to the fields on the model
Parameters: raw_data – The data to be converted
-
flatten
(role=None, prefix='')¶ Return data as a pure key-value dictionary, where the values are primitive types (string, bool, int, long).
Parameters: - role – Filter output by a specific role
- prefix – A prefix to use for keynames during flattening.
-
classmethod
get_mock_object
(context=None, overrides=None)¶ Get a mock object.
Parameters: - context (dict) –
- overrides (dict) – overrides for the model
-
import_data
(raw_data, **kw)¶ Converts and imports the raw data into the instance of the model according to the fields in the model.
Parameters: raw_data – The data to be imported.
-
to_primitive
(role=None, context=None)¶ Return data as it would be validated. No filtering of output unless role is defined.
Parameters: role – Filter output by a specific role
-
validate
(partial=False, strict=False)¶ Validates the state of the model and adding additional untrusted data as well. If the models is invalid, raises ValidationError with error messages.
Parameters: - partial – Allow partial data to validate; useful for PATCH requests.
Essentially drops the
required=True
arguments from field definitions. Default: False - strict – Complain about unrecognized keys. Default: False
- partial – Allow partial data to validate; useful for PATCH requests.
Essentially drops the
-
classmethod
-
class
schematics.models.
ModelMeta
¶ Meta class for Models.
-
class
schematics.models.
ModelOptions
(klass, namespace=None, roles=None, serialize_when_none=True, fields_order=None)¶ This class is a container for all metaclass configuration options. Its primary purpose is to create an instance of a model’s options for every instance of a model.
Usage¶
To learn more about how Models are used, visit Using Models