Serializer Options

Flex-field options can be supplied in three places. For request semantics and precedence rules, see Core Concepts.

1. Query parameters

GET /people?expand=friends.hobbies,employer&omit=age

2. Serializer constructor keyword arguments

serializer = PersonSerializer(
    person,
    expand=["friends.hobbies", "employer"],
    omit=["friends.age"],
)

3. Nested serializer settings in expandable_fields

from rest_flex_fields2.serializers import FlexFieldsModelSerializer


class PersonSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = Person
        fields = ["age", "hobbies", "name"]
        expandable_fields = {
            "friends": (
                "serializer.FriendSerializer",
                {"many": True, "expand": ["hobbies"], "omit": ["age"]},
            ),
        }

Supported options

Option

Description

expand

Fields to expand. They must be configured in expandable_fields.

fields

Fields to include. All other fields are excluded.

omit

Fields to exclude. All other fields are included.

Query-parameter values accept comma-separated lists. Nested paths use dot notation.

For concrete request and response examples, see Usage.