OpenQuizz
Une application de gestion des contenus pédagogiques
Field Class Reference
Inheritance diagram for Field:
Collaboration diagram for Field:

Public Member Functions

None __init__ (self, *typing.Any default=missing_, typing.Any missing=missing_, typing.Optional[str] data_key=None, typing.Optional[str] attribute=None, typing.Optional[typing.Union[typing.Callable[[typing.Any], typing.Any], typing.Iterable[typing.Callable[[typing.Any], typing.Any]],]] validate=None, bool required=False, typing.Optional[bool] allow_none=None, bool load_only=False, bool dump_only=False, typing.Optional[typing.Dict[str, str]] error_messages=None, typing.Optional[typing.Mapping[str, typing.Any]] metadata=None, **additional_metadata)
 
str __repr__ (self)
 
def __deepcopy__ (self, memo)
 
def get_value (self, obj, attr, accessor=None, default=missing_)
 
ValidationError make_error (self, str key, **kwargs)
 
def fail (self, str key, **kwargs)
 
def serialize (self, str attr, typing.Any obj, typing.Optional[typing.Callable[[typing.Any, str, typing.Any], typing.Any]] accessor=None, **kwargs)
 
def deserialize (self, typing.Any value, typing.Optional[str] attr=None, typing.Optional[typing.Mapping[str, typing.Any]] data=None, **kwargs)
 
def context (self)
 
- Public Member Functions inherited from FieldABC
def serialize (self, attr, obj, accessor=None)
 
def deserialize (self, value)
 

Data Fields

 default
 
 attribute
 
 data_key
 
 validate
 
 validators
 
 allow_none
 
 load_only
 
 dump_only
 
 required
 
 missing
 
 metadata
 
 error_messages
 
 parent
 
 name
 
 root
 

Static Public Attributes

 default_error_messages
 
- Static Public Attributes inherited from FieldABC
 parent
 
 name
 
 root
 

Detailed Description

Basic field from which other fields should extend. It applies no
formatting by default, and should only be used in cases where
data does not need to be formatted before being serialized or deserialized.
On error, the name of the field will be returned.

:param default: If set, this value will be used during serialization if the input value
    is missing. If not set, the field will be excluded from the serialized output if the
    input value is missing. May be a value or a callable.
:param missing: Default deserialization value for the field if the field is not
    found in the input data. May be a value or a callable.
:param data_key: The name of the dict key in the external representation, i.e.
    the input of `load` and the output of `dump`.
    If `None`, the key will match the name of the field.
:param attribute: The name of the attribute to get the value from when serializing.
    If `None`, assumes the attribute has the same name as the field.
    Note: This should only be used for very specific use cases such as
    outputting multiple fields for a single attribute. In most cases,
    you should use ``data_key`` instead.
:param validate: Validator or collection of validators that are called
    during deserialization. Validator takes a field's input value as
    its only parameter and returns a boolean.
    If it returns `False`, an :exc:`ValidationError` is raised.
:param required: Raise a :exc:`ValidationError` if the field value
    is not supplied during deserialization.
:param allow_none: Set this to `True` if `None` should be considered a valid value during
    validation/deserialization. If ``missing=None`` and ``allow_none`` is unset,
    will default to ``True``. Otherwise, the default is ``False``.
:param load_only: If `True` skip this field during serialization, otherwise
    its value will be present in the serialized data.
:param dump_only: If `True` skip this field during deserialization, otherwise
    its value will be present in the deserialized object. In the context of an
    HTTP API, this effectively marks the field as "read-only".
:param dict error_messages: Overrides for `Field.default_error_messages`.
:param metadata: Extra information to be stored as field metadata.

.. versionchanged:: 2.0.0
    Removed `error` parameter. Use ``error_messages`` instead.

.. versionchanged:: 2.0.0
    Added `allow_none` parameter, which makes validation/deserialization of `None`
    consistent across fields.

.. versionchanged:: 2.0.0
    Added `load_only` and `dump_only` parameters, which allow field skipping
    during the (de)serialization process.

.. versionchanged:: 2.0.0
    Added `missing` parameter, which indicates the value for a field if the field
    is not found during deserialization.

.. versionchanged:: 2.0.0
    ``default`` value is only used if explicitly set. Otherwise, missing values
    inputs are excluded from serialized output.

.. versionchanged:: 3.0.0b8
    Add ``data_key`` parameter for the specifying the key in the input and
    output data. This parameter replaced both ``load_from`` and ``dump_to``.

Constructor & Destructor Documentation

◆ __init__()

None __init__ (   self,
*typing.Any   default = missing_,
typing.Any   missing = missing_,
typing.Optional[str]   data_key = None,
typing.Optional[str]   attribute = None,
typing.Optional[ typing.Union[ typing.Callable[[typing.Any], typing.Any], typing.Iterable[typing.Callable[[typing.Any], typing.Any]], ] ]   validate = None,
bool   required = False,
typing.Optional[bool]   allow_none = None,
bool   load_only = False,
bool   dump_only = False,
typing.Optional[typing.Dict[str, str]]   error_messages = None,
typing.Optional[typing.Mapping[str, typing.Any]]   metadata = None,
**  additional_metadata 
)

Member Function Documentation

◆ __deepcopy__()

def __deepcopy__ (   self,
  memo 
)

◆ __repr__()

str __repr__ (   self)

◆ context()

def context (   self)
The context dictionary for the parent :class:`Schema`.

◆ deserialize()

def deserialize (   self,
typing.Any  value,
typing.Optional[str]   attr = None,
typing.Optional[typing.Mapping[str, typing.Any]]   data = None,
**  kwargs 
)
Deserialize ``value``.

:param value: The value to deserialize.
:param attr: The attribute/key in `data` to deserialize.
:param data: The raw input data passed to `Schema.load`.
:param kwargs: Field-specific keyword arguments.
:raise ValidationError: If an invalid value is passed or if a required value
    is missing.

◆ fail()

def fail (   self,
str  key,
**  kwargs 
)
Helper method that raises a `ValidationError` with an error message
from ``self.error_messages``.

.. deprecated:: 3.0.0
    Use `make_error <marshmallow.fields.Field.make_error>` instead.

◆ get_value()

def get_value (   self,
  obj,
  attr,
  accessor = None,
  default = missing_ 
)
Return the value for a given key from an object.

:param object obj: The object to get the value from.
:param str attr: The attribute/key in `obj` to get the value from.
:param callable accessor: A callable used to retrieve the value of `attr` from
    the object `obj`. Defaults to `marshmallow.utils.get_value`.

◆ make_error()

ValidationError make_error (   self,
str  key,
**  kwargs 
)
Helper method to make a `ValidationError` with an error message
from ``self.error_messages``.

◆ serialize()

def serialize (   self,
str  attr,
typing.Any  obj,
typing.Optional[ typing.Callable[[typing.Any, str, typing.Any], typing.Any] ]   accessor = None,
**  kwargs 
)
Pulls the value for the given key from the object, applies the
field's formatting and returns the result.

:param attr: The attribute/key to get from the object.
:param obj: The object to access the attribute/key from.
:param accessor: Function used to access values from ``obj``.
:param kwargs: Field-specific keyword arguments.

Field Documentation

◆ allow_none

allow_none

◆ attribute

attribute

◆ data_key

data_key

◆ default

default

◆ default_error_messages

default_error_messages
static

◆ dump_only

dump_only

◆ error_messages

error_messages

◆ load_only

load_only

◆ metadata

metadata

◆ missing

missing

◆ name

name

◆ parent

parent

◆ required

required

◆ root

root

◆ validate

validate

◆ validators

validators

The documentation for this class was generated from the following file: