Skip to main content

Flexible, dynamic fields and nested resources for Django REST Framework serializers (forked from drf-flex-fields).

Project description

drf-flex-fields2

Package version

Flexible dynamic fields and nested resources for Django REST Framework serializers.

This project is for people building APIs, people integrating them, and people maintaining the ecosystem around them. If you are new to flexible serializers, welcome. If you are evaluating this for production, welcome. If you want to contribute fixes, docs, tests, or ideas, welcome.

  1. Migration from drf-flex-fields
  2. Documentation
  3. Installation
  4. Quick Example
  5. Highlights
  6. License
  7. History

Migration from drf-flex-fields

This is a fork of drf-flex-fields developed and maintained by Robert Singer between 2018 and 2023. For more details on why this fork exists, see History below. See the Migration Guide in the documentation for detailed instructions. The short version is:

  1. Upgrade Django and DRF dependencies, if not done already.
  2. Install drf-flex-fields2 instead of drf-flex-fields.
  3. Fix package name in import paths: rest_flex_fields2 instead of rest_flex_fields
  4. Change package-level imports to deep imports, e.g.: from rest_flex_fields2.serializers import FlexFieldsModelSerializer
  5. Rename REST_FLEX_FIELDS to REST_FLEX_FIELDS2 in Django settings.

The drf-flex-fields2 API is stable and compatible with the original drf-flex-fields package. There are currently no plans to break the existing API. However, if breaking changes become necessary in the future, they will follow semantic versioning guidelines and the major version number will be incremented accordingly.

Users, community contributors, and maintainers are warmly welcome to keep this package useful and maintained.

Documentation

The full documentation is published on Read the Docs: https://drf-flex-fields2.readthedocs.io/

Installation

pip install drf-flex-fields2

Quick Example

from rest_flex_fields2.serializers import FlexFieldsModelSerializer


class StateSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = State
        fields = ("id", "name")


class CountrySerializer(FlexFieldsModelSerializer):
    class Meta:
        model = Country
        fields = ("id", "name", "population", "states")
        expandable_fields = {
            "states": (StateSerializer, {"many": True}),
        }


class PersonSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = Person
        fields = ("id", "name", "country", "occupation")
        expandable_fields = {
            "country": CountrySerializer,
        }

Default response:

{
    "id": 142,
    "name": "Jim Halpert",
    "country": 1
}

Expanded response for GET /people/142/?expand=country.states:

{
    "id": 142,
    "name": "Jim Halpert",
    "country": {
        "id": 1,
        "name": "United States",
        "states": [
            {
                "id": 23,
                "name": "Ohio"
            },
            {
                "id": 2,
                "name": "Pennsylvania"
            }
        ]
    }
}

Highlights

  • Expand nested relations with ?expand=.
  • Limit response payloads with ?fields= and ?omit=.
  • Use dot notation for nested expansion and sparse fieldsets.
  • Reuse serializers by passing expand, fields, and omit directly.

License

MIT. See LICENSE.md.

History

The original drf-flex-fields was developed and maintained by Robert Singer between 2018 and 2023. However, in 2023 maintenance appeared to stop with no further commits and issues and pull-requests remaining unanswered.

In March 2026, Django REST Framework 3.17.0 removed coreapi support, which unfortunately broke the existing package. Although the immediate fix was simple, the project was due for broader modernization, including tooling updates, Python 2 to 3 cleanup, dependency version maintenance and proper documentation.

This fork exists because drf-flex-fields is used in the OpenBook project, and we want to reduce supply-chain risk from outdated dependencies while keeping this package healthy and maintained. Please join the community and help us with this mission. Oh, and keep your own packages up to date and maintained, will you? :-)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_flex_fields2-2.0.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_flex_fields2-2.0.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file drf_flex_fields2-2.0.0.tar.gz.

File metadata

  • Download URL: drf_flex_fields2-2.0.0.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.12 Linux/6.19.8-100.fc42.x86_64

File hashes

Hashes for drf_flex_fields2-2.0.0.tar.gz
Algorithm Hash digest
SHA256 2f340a113f43c7e3374e1eb8f332a425f0adff731c29eeb951b71de9f0a75a97
MD5 b1f6a711a76981558082d5abbc346d81
BLAKE2b-256 3d5a8a2ddc38b5bceb94cf11ec9a6e2d3cb81265036fe1b66c45db238117034c

See more details on using hashes here.

File details

Details for the file drf_flex_fields2-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: drf_flex_fields2-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.12 Linux/6.19.8-100.fc42.x86_64

File hashes

Hashes for drf_flex_fields2-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 716eddf9884a5ac7f4baf430bc78b24365aafe7128a9dcc77d77c68a19595c35
MD5 8a54fdb1290cf7d5db2a7f17e59e39b1
BLAKE2b-256 528a1fd5f00d8fe6952cf31f7bc0e7278dc2c1bff2a9a33e48781361ca65fde7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page