---
url: "https://vueda.dev/v3/reference/api/py/vueda.core.serializers/GenericForeignKeySerializer.md"
title: "GenericForeignKeySerializer"
---

# 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` {#get_fields}

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

### Signature {#get_fields-signature}

`get_fields(self)`

#### Parameters {#get_fields-parameters}

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| self |  | yes |  |  |

### Source {#get_fields-source}

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

## `to_representation` {#to_representation}

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

### Signature {#to_representation-signature}

`to_representation(self, instance)`

#### Parameters {#to_representation-parameters}

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| self |  | yes |  |  |
| <span id="to_representation-param-instance">instance</span> |  | yes |  |  |

### Source {#to_representation-source}

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

## Source

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