Skip to content

GenericForeignKeySerializer

Overview

Serializer for GenericForeignKey expand fields.

Dynamically serializes the related object's concrete fields at to_representation time, since the related model is unknown until then. Supports flex field filtering (fields/omit) via rest_flex_fields options passed through expandable_fields.

The instance is not available at get_fields() time — for nested serializers DRF passes the related value directly to to_representation(), never setting self.instance. All dynamic field logic therefore lives in to_representation().

Always includes app_label, model, and formatted_name in the output regardless of field filtering.

Model-targeted field specifiers (_applabel__modelname__field) may be used in the FIELDS_PARAM and OMIT_PARAM lists inside expandable_fields to apply filtering only when the related object is an instance of the named model. Plain field names and wildcards apply to every related model type. If no static filtering is needed, GenericForeignKeySerializer may be declared as a bare class without options.

get_fields

Returns a dictionary of {field_name: field_instance}.

Signature

get_fields(self)

Parameters

NameTypeRequiredDescription
selfyes

Source

server/vueda/core/serializers/__init__.py:875

to_representation

Object instance -> Dict of primitive datatypes.

Signature

to_representation(self, instance)

Parameters

NameTypeRequiredDescription
selfyes
instanceyes

Source

server/vueda/core/serializers/__init__.py:879

_declared_fields

Source

server/vueda/core/serializers/__init__.py:853

Documents matching: server v3.0.0a1.post1client v3.0.0-alpha.2