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_fields2 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 ​

Return fields after applying constructor-sourced flex-fields options.

Signature ​

get_fields(self)

Parameters ​

NameTypeRequiredDefaultDescription
selfyes

Source ​

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

to_representation ​

Apply request-sourced flex-fields options once, then delegate to super.

Signature ​

to_representation(self, instance)

Parameters ​

NameTypeRequiredDefaultDescription
selfyes
instanceyes

Source ​

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

Source ​

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