Skip to main content

Flexible, dynamic fields and nested models for ADRF (Async Django REST Framework) serializers

Project description

ADRF Flex Fields

Flexible, dynamic fields and nested models for ADRF (Async Django REST Framework) serializers.

Description

ADRF Flex Fields extends the Async Django REST Framework (ADRF) with powerful field expansion and dynamic serialization capabilities. This package allows you to create more flexible APIs by enabling clients to specify which fields they want to include in responses, reducing over-fetching and improving performance.

Features

  • Dynamic Field Selection: Allow clients to specify which fields to include/exclude
  • Nested Model Expansion: Expand related models on-demand
  • Async Support: Full compatibility with ADRF's async capabilities
  • Performance Optimized: Reduce payload size and database queries
  • Easy Integration: Drop-in replacement for standard ADRF serializers

Installation

pip install adrf_flex_fields

Quick Start

Basic Usage

from adrf_flex_fields import FlexFieldsModelSerializer
from adrf import serializers
from myapp.models import User, Profile

class UserSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'email', 'profile']
        expandable_fields = {
            'profile': ProfileSerializer
        }

# Usage in views
class UserViewSet(AsyncModelViewSet):
    serializer_class = UserSerializer
    queryset = User.objects.all()

Field Selection

Clients can control which fields are returned:

# Get only id and username
GET /api/users/?fields=id,username

# Exclude email field
GET /api/users/?omit=email

# Expand nested profile
GET /api/users/?expand=profile

Advanced Features

class UserSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'email', 'profile', 'posts']
        expandable_fields = {
            'profile': ProfileSerializer,
            'posts': (PostSerializer, {'many': True})
        }

    # Custom field filtering
    def get_field_names(self, declared_fields, info):
        field_names = super().get_field_names(declared_fields, info)
        # Custom logic here
        return field_names

Requirements

  • Python >= 3.8
  • Django >= 4.1
  • djangorestframework >= 3.14
  • adrf >= 0.1.0

Development

Setup Development Environment

git clone https://github.com/mushota_243/adrf-flex-fields.git
cd adrf-flex-fields
pip install -e ".[dev]"

Running Tests

pytest

Running Tests with Coverage

pytest --cov=adrf_flex_fields --cov-report=html

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

0.1.0 (2024-01-20)

  • Initial release
  • Basic field selection and expansion functionality
  • Async Django REST Framework compatibility
  • Property-based testing suite

Support

If you encounter any issues or have questions, please open an issue on GitHub.

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

adrf_flex_fields-0.1.0.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

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

adrf_flex_fields-0.1.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file adrf_flex_fields-0.1.0.tar.gz.

File metadata

  • Download URL: adrf_flex_fields-0.1.0.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for adrf_flex_fields-0.1.0.tar.gz
Algorithm Hash digest
SHA256 25f73c31331473f37f346cdb9299a8354f7ee9a66cffb2dbee2f40a79d1a8214
MD5 cd2b2bf25cbe9c373e5acf6253b371be
BLAKE2b-256 04cd9fcad6f5c118c54e02b483197a22747bdcfe06cb59e375aeb86ff4a2bdf0

See more details on using hashes here.

File details

Details for the file adrf_flex_fields-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for adrf_flex_fields-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50b172a4c5a9cfbbe85bceb9f0434605030c9740e35b96c46c1cfed8359230f8
MD5 199ce239d62de6e775797721013cbe12
BLAKE2b-256 5fc3845f901e47670a1de8eda56a58b04898ff359d34cb16025ccbfdb57422b5

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