deseasion.backend.schemas.base module
- class deseasion.backend.schemas.base.BaseSchema(*args, **kwargs)
Bases:
SQLAlchemyAutoSchema
This is the base class for all schemas converting Json and SQL Models.
There are multiple cases for schemas:
One schema for all CRU operations: then simply put all fields
Two schemas for update and retrieval: then make one schema with non-modifiable fields as dump_only in
Meta
Two schemas for creation and retrieval: then make one schema with non-created fields as dump_only in
Meta
(can also add write-only fields in load_only)Three schemas for creation, update and retrieval: split creation schema, then make other schema as an update/retrieval shared schema
Careful when associating a resource within another, as the update of one could update the other. If a single schema is used to represent both a resource as itself and the same one nested, the id field will be broken (non-writable for the resource as itself, writable for the nesting resource).
Note
Fields with foreign keys are not created by default
Relationship fields are not created by default
FIelds added manually are not required and non-nullable by default
- class Meta
Bases:
object
- load_instance = True
- sqla_session = <sqlalchemy.orm.scoping.scoped_session object>
- strict = True
- get_future_field(data, key)
Return field value (from instance or serialized data).
- Parameters:
data – serialized data that will update instance
key – field name
- Returns:
value
- opts: SchemaOpts = <marshmallow_sqlalchemy.schema.SQLAlchemyAutoSchemaOpts object>
- class deseasion.backend.schemas.base.FieldLookupNested(nested: ~marshmallow.base.SchemaABC | type | str | dict[str, ~marshmallow.fields.Field | type] | ~typing.Callable[[], ~marshmallow.base.SchemaABC | type | dict[str, ~marshmallow.fields.Field | type]], *, dump_default: ~typing.Any = <marshmallow.missing>, default: ~typing.Any = <marshmallow.missing>, only: ~typing.Sequence[str] | ~typing.AbstractSet[str] | None = None, exclude: ~typing.Sequence[str] | ~typing.AbstractSet[str] = (), many: bool = False, unknown: str | None = None, **kwargs)
Bases:
Nested
Nested field which will try to load an existing object.
https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/117
- default_error_messages = {'missing': 'Could not find related object {input}.', 'type': 'Invalid input type. Expected a list.'}
Default error messages.
- get_instance_or_fail(data)
- class deseasion.backend.schemas.base.FieldMethod(spec_field: Field, serialize: str | None = None, deserialize: str | None = None, **kwargs)
Bases:
Method
This class represents a field method with an alternative field to use as source for OpenAPI specification.
- Parameters:
spec_field – alternative field to use as source for openAPI specification
serialize – serialization method used
deserialize – deserialization method used
- class deseasion.backend.schemas.base.FieldPluck(model, field_name, **kwargs)
Bases:
Pluck
This class creates a pluck field for a SQL model.
It creates a new schema for this model, only containing the field to pluck.
- Parameters:
model – SQL model class to pluck
field_name – field to pluck
- class deseasion.backend.schemas.base.OneOfSchema(*args, many=False, **kwargs)
Bases:
OneOfSchema
This class is a wrapper for
marshmallow_oneofschema.OneOfSchema
.It adds one feature necessary to this backend:
Pass schema constructor args and kwargs to typed schemas
- opts: SchemaOpts = <marshmallow.schema.SchemaOpts object>
- class deseasion.backend.schemas.base.OneOfSchemaWithType(*args, many=False, **kwargs)
Bases:
OneOfSchema
This class is a wrapper for
marshmallow_oneofschema.OneOfSchema
.It is to be used for schemas needing the type to appear in the openapi specification while being removed upon deserialization. This is typically the case for oneOf creation schemas.
- opts: SchemaOpts = <marshmallow.schema.SchemaOpts object>
- deseasion.backend.schemas.base.constant_enum(value) type[Enum]
This creates an enum with a single value.
Its use should be limited to schema polymorphism so a type discriminator which is an enumeration of all possible types (children types) is set as only the child type in the child.
- Parameters:
value – either a single enum item or any object
- Returns:
enumeration class with only value as item
Note
might override a String field as well if the parent class type discriminator field was left open for future appendings, this is generally how schema polymorphism is implemented in OpenAPI 3.0+. In this case, the closed enumeration must be done in the oneOf classes.