geonature.core.gn_commons.schemas¶
Attributes¶
Classes¶
SQLAlchemyAutoSchema that automatically generates marshmallow fields |
|
SQLAlchemyAutoSchema that automatically generates marshmallow fields |
|
SQLAlchemyAutoSchema that automatically generates marshmallow fields |
|
SQLAlchemyAutoSchema that automatically generates marshmallow fields |
|
Base schema class with which to define schemas. |
|
A field which tries to cast the value to int or float before returning it. |
|
This mixin automatically exclude from serialization: |
Module Contents¶
- class geonature.core.gn_commons.schemas.ModuleSchema(*args, **kwargs)[source]¶
Bases:
geonature.utils.env.MA.SQLAlchemyAutoSchemaSQLAlchemyAutoSchema that automatically generates marshmallow fields from a SQLAlchemy model’s or table’s column. Uses the scoped session from Flask-SQLAlchemy by default.
See marshmallow_sqlalchemy.SQLAlchemyAutoSchema for more details on the SQLAlchemyAutoSchema API.
- class Meta[source]¶
Options object for a Schema.
Example usage:
from marshmallow import Schema class MySchema(Schema): class Meta: fields = ("id", "email", "date_created") exclude = ("password", "secret_attribute")
A note on type checking
Type checkers will only check the attributes of the Meta <marshmallow.Schema.Meta> class if you explicitly subclass marshmallow.Schema.Meta.
from marshmallow import Schema class MySchema(Schema): # Not checked by type checkers class Meta: additional = True class MySchema2(Schema): # Type checkers will check attributes class Meta(Schema.Meta): additional = True # Incompatible types in assignment
Supprimé dans la version 3.0.0b7: Remove
strict.Ajouté dans la version 3.0.0b12: Add unknown.
Modifié dans la version 3.0.0b17: Rename
dateformatto datetimeformat.Ajouté dans la version 3.9.0: Add timeformat.
Modifié dans la version 3.26.0: Deprecate
ordered. Field order is preserved by default.Supprimé dans la version 4.0.0: Remove
ordered.
- class geonature.core.gn_commons.schemas.MediaSchema(*args, **kwargs)[source]¶
Bases:
geonature.utils.env.MA.SQLAlchemyAutoSchemaSQLAlchemyAutoSchema that automatically generates marshmallow fields from a SQLAlchemy model’s or table’s column. Uses the scoped session from Flask-SQLAlchemy by default.
See marshmallow_sqlalchemy.SQLAlchemyAutoSchema for more details on the SQLAlchemyAutoSchema API.
- class Meta[source]¶
Options object for a Schema.
Example usage:
from marshmallow import Schema class MySchema(Schema): class Meta: fields = ("id", "email", "date_created") exclude = ("password", "secret_attribute")
A note on type checking
Type checkers will only check the attributes of the Meta <marshmallow.Schema.Meta> class if you explicitly subclass marshmallow.Schema.Meta.
from marshmallow import Schema class MySchema(Schema): # Not checked by type checkers class Meta: additional = True class MySchema2(Schema): # Type checkers will check attributes class Meta(Schema.Meta): additional = True # Incompatible types in assignment
Supprimé dans la version 3.0.0b7: Remove
strict.Ajouté dans la version 3.0.0b12: Add unknown.
Modifié dans la version 3.0.0b17: Rename
dateformatto datetimeformat.Ajouté dans la version 3.9.0: Add timeformat.
Modifié dans la version 3.26.0: Deprecate
ordered. Field order is preserved by default.Supprimé dans la version 4.0.0: Remove
ordered.
- class geonature.core.gn_commons.schemas.TValidationSchema(*args, **kwargs)[source]¶
Bases:
geonature.utils.env.MA.SQLAlchemyAutoSchemaSQLAlchemyAutoSchema that automatically generates marshmallow fields from a SQLAlchemy model’s or table’s column. Uses the scoped session from Flask-SQLAlchemy by default.
See marshmallow_sqlalchemy.SQLAlchemyAutoSchema for more details on the SQLAlchemyAutoSchema API.
- class Meta[source]¶
Options object for a Schema.
Example usage:
from marshmallow import Schema class MySchema(Schema): class Meta: fields = ("id", "email", "date_created") exclude = ("password", "secret_attribute")
A note on type checking
Type checkers will only check the attributes of the Meta <marshmallow.Schema.Meta> class if you explicitly subclass marshmallow.Schema.Meta.
from marshmallow import Schema class MySchema(Schema): # Not checked by type checkers class Meta: additional = True class MySchema2(Schema): # Type checkers will check attributes class Meta(Schema.Meta): additional = True # Incompatible types in assignment
Supprimé dans la version 3.0.0b7: Remove
strict.Ajouté dans la version 3.0.0b12: Add unknown.
Modifié dans la version 3.0.0b17: Rename
dateformatto datetimeformat.Ajouté dans la version 3.9.0: Add timeformat.
Modifié dans la version 3.26.0: Deprecate
ordered. Field order is preserved by default.Supprimé dans la version 4.0.0: Remove
ordered.
- class geonature.core.gn_commons.schemas.BibWidgetSchema(*args, **kwargs)[source]¶
Bases:
geonature.utils.env.MA.SQLAlchemyAutoSchemaSQLAlchemyAutoSchema that automatically generates marshmallow fields from a SQLAlchemy model’s or table’s column. Uses the scoped session from Flask-SQLAlchemy by default.
See marshmallow_sqlalchemy.SQLAlchemyAutoSchema for more details on the SQLAlchemyAutoSchema API.
- class Meta[source]¶
Options object for a Schema.
Example usage:
from marshmallow import Schema class MySchema(Schema): class Meta: fields = ("id", "email", "date_created") exclude = ("password", "secret_attribute")
A note on type checking
Type checkers will only check the attributes of the Meta <marshmallow.Schema.Meta> class if you explicitly subclass marshmallow.Schema.Meta.
from marshmallow import Schema class MySchema(Schema): # Not checked by type checkers class Meta: additional = True class MySchema2(Schema): # Type checkers will check attributes class Meta(Schema.Meta): additional = True # Incompatible types in assignment
Supprimé dans la version 3.0.0b7: Remove
strict.Ajouté dans la version 3.0.0b12: Add unknown.
Modifié dans la version 3.0.0b17: Rename
dateformatto datetimeformat.Ajouté dans la version 3.9.0: Add timeformat.
Modifié dans la version 3.26.0: Deprecate
ordered. Field order is preserved by default.Supprimé dans la version 4.0.0: Remove
ordered.
- class geonature.core.gn_commons.schemas.LabelValueDict(*, only: marshmallow.types.StrSequenceOrSet | None = None, exclude: marshmallow.types.StrSequenceOrSet = (), many: bool | None = None, load_only: marshmallow.types.StrSequenceOrSet = (), dump_only: marshmallow.types.StrSequenceOrSet = (), partial: bool | marshmallow.types.StrSequenceOrSet | None = None, unknown: marshmallow.types.UnknownOption | None = None)[source]¶
Bases:
marshmallow.SchemaBase schema class with which to define schemas.
Example usage:
import datetime as dt from dataclasses import dataclass from marshmallow import Schema, fields @dataclass class Album: title: str release_date: dt.date class AlbumSchema(Schema): title = fields.Str() release_date = fields.Date() album = Album("Beggars Banquet", dt.date(1968, 12, 6)) schema = AlbumSchema() data = schema.dump(album) data # {'release_date': '1968-12-06', 'title': 'Beggars Banquet'}
- Paramètres:
only – Whitelist of the declared fields to select when instantiating the Schema. If None, all fields are used. Nested fields can be represented with dot delimiters.
exclude – Blacklist of the declared fields to exclude when instantiating the Schema. If a field appears in both only and exclude, it is not used. Nested fields can be represented with dot delimiters.
many – Should be set to True if
objis a collection so that the object will be serialized to a list.load_only – Fields to skip during serialization (write-only fields)
dump_only – Fields to skip during deserialization (read-only fields)
partial – Whether to ignore missing fields and not require any fields declared. Propagates down to
Nestedfields as well. If its value is an iterable, only missing fields listed in that iterable will be ignored. Use dot delimiters to specify nested fields.unknown – Whether to exclude, include, or raise an error for unknown fields in the data. Use EXCLUDE, INCLUDE or RAISE.
Modifié dans la version 3.0.0: Remove
prefixparameter.Modifié dans la version 4.0.0: Remove
contextparameter.
- class geonature.core.gn_commons.schemas.CastableField(*, load_default: Any = missing_, dump_default: Any = missing_, data_key: str | None = None, attribute: str | None = None, validate: marshmallow.types.Validator | Iterable[marshmallow.types.Validator] | None = None, pre_load: marshmallow.types.PreLoadCallable | Iterable[marshmallow.types.PreLoadCallable] | None = None, post_load: marshmallow.types.PostLoadCallable | Iterable[marshmallow.types.PostLoadCallable] | None = None, required: bool = False, allow_none: bool | None = None, load_only: bool = False, dump_only: bool = False, error_messages: marshmallow.types.ErrorMessages | None = None, metadata: Mapping[str, Any] | None = None)[source]¶
Bases:
marshmallow.fields.FieldA field which tries to cast the value to int or float before returning it. If the value is not castable, the default value is returned.
- _serialize(value, attr, obj, **kwargs)[source]¶
Serializes
valueto a basic Python datatype. Noop by default. ConcreteFieldclasses should implement this method.Example:
class TitleCase(Field): def _serialize(self, value, attr, obj, **kwargs): if not value: return "" return str(value).title()
- Paramètres:
value – The value to be serialized.
attr – The attribute or key on the object to be serialized.
obj – The object the value was pulled from.
kwargs – Field-specific keyword arguments.
- Renvoie:
The serialized value
- class geonature.core.gn_commons.schemas.TAdditionalFieldsSchema(*args, **kwargs)[source]¶
Bases:
utils_flask_sqla.schema.SmartRelationshipsMixin,geonature.utils.env.MA.SQLAlchemyAutoSchemaThis mixin automatically exclude from serialization:
Nested, RelatedList and Related fields
all fields with exclude=True in their metadata (e.g.
fields.String(metadata={'exclude': True}))For SQLAlchemyAutoSchema, all deffered columns
Default marshmallow behaviour is to serialize only fields specified in
only. This mixin add these:If they are only Nested fields in
only, they are added to serialized fields along with default serialized fields.To serialize Nested fields only and not default serialized fields, add
"-"special value toonly.To add excluded fields (through metadata or deffered fields) to the serialization in addition to default serialized fields, use “+field_name” syntax.
Examples :
class FooSchema(SmartRelationshipsMixin): id = fields.Int() name = field.Str() default_excluded_field = fields.Str(metadata={"exclude": True}) relationship = fields.Nested(OtherSchema) # or field.RelatedList() / field.Related() FooSchema().dump() -> {"id": 1, "name": "toto" } FooSchema(only=["id"]).dump() -> {"id": 1} FooSchema(only=["default_excluded_field"]).dump() -> {"default_excluded_field": "test"} FooSchema(only=["+default_excluded_field"]).dump() -> {"id": 1, "name": "toto", "default_excluded_field": "test"} FooSchema(only=["relationship"]).dump() -> {"id": 1, "name": "toto", "relationship": {OtherSchema...} } FooSchema(only=["id", "relationship"]).dump() -> {"id": 1, "relationship": {OtherSchema...}} FooSchema(only=["-", "relationship"]).dump() -> {"relationship": {OtherSchema...}}
- class Meta[source]¶
Options object for a Schema.
Example usage:
from marshmallow import Schema class MySchema(Schema): class Meta: fields = ("id", "email", "date_created") exclude = ("password", "secret_attribute")
A note on type checking
Type checkers will only check the attributes of the Meta <marshmallow.Schema.Meta> class if you explicitly subclass marshmallow.Schema.Meta.
from marshmallow import Schema class MySchema(Schema): # Not checked by type checkers class Meta: additional = True class MySchema2(Schema): # Type checkers will check attributes class Meta(Schema.Meta): additional = True # Incompatible types in assignment
Supprimé dans la version 3.0.0b7: Remove
strict.Ajouté dans la version 3.0.0b12: Add unknown.
Modifié dans la version 3.0.0b17: Rename
dateformatto datetimeformat.Ajouté dans la version 3.9.0: Add timeformat.
Modifié dans la version 3.26.0: Deprecate
ordered. Field order is preserved by default.Supprimé dans la version 4.0.0: Remove
ordered.
- load(data, *, many=None, **kwargs)[source]¶
Deserialize data. If
load_instanceis set to True in the schema meta options, load the data as model instance(s).- Paramètres:
data – The data to deserialize.
session – SQLAlchemy session <sqlalchemy.orm.Session>.
instance – Existing model instance to modify.
transient – If True, load transient model instance(s).
kwargs – Same keyword arguments as marshmallow.Schema.load.