rest_flex_fields2.serializers.FlexFieldsSerializerMixin ======================================================= .. py:class:: rest_flex_fields2.serializers.FlexFieldsSerializerMixin(*args, **kwargs) Bases: :py:obj:`rest_framework.serializers.Serializer` Mixin that adds sparse-fieldset and nested-expansion support to a serializer. Accepts the ``fields``, ``omit``, and ``expand`` keyword arguments (names are configurable via ``REST_FLEX_FIELDS2`` settings) both as constructor kwargs and as query-string parameters on the current request. Query parameters are only read on the root serializer; nested serializers receive their options through constructor kwargs propagated by the parent. Declare expandable relations either on ``Meta.expandable_fields`` (preferred) or directly on ``expandable_fields`` for backwards compatibility. .. py:attribute:: expandable_fields .. py:attribute:: maximum_expansion_depth :type: Optional[int] :value: None .. py:attribute:: recursive_expansion_permitted :type: Optional[bool] :value: None .. py:attribute:: parent .. py:attribute:: expanded_fields :value: [] .. py:method:: get_maximum_expansion_depth() Return the effective maximum expansion depth. Uses the serializer-level ``maximum_expansion_depth`` attribute when set, otherwise falls back to the ``MAXIMUM_EXPANSION_DEPTH`` setting. .. py:method:: get_recursive_expansion_permitted() Return whether recursive expansion is allowed. Uses the serializer-level ``recursive_expansion_permitted`` attribute when set, otherwise falls back to the ``RECURSIVE_EXPANSION_PERMITTED`` setting. .. py:method:: to_representation(instance) Apply request-sourced flex-fields options once, then delegate to super. .. py:method:: get_fields() Return fields after applying constructor-sourced flex-fields options. .. py:method:: apply_flex_fields(fields, flex_options) Apply sparse-fieldset and expansion options to `fields` in place. Removes fields that are excluded by ``omit`` or not present in ``fields`` (sparse-fieldset), then replaces fields listed in ``expand`` with their nested serializer instances. Returns the modified `fields` mapping. .. py:method:: recursive_expansion_not_permitted() Raise a validation error indicating recursive expansion. Override this method to raise a custom exception instead of the default ``ValidationError``. .. py:method:: expansion_depth_exceeded() Raise a validation error indicating the expansion depth limit was exceeded. Override this method to raise a custom exception instead of the default ``ValidationError``.