Skip to main content

This package provides utils for Django Rest Framework API views and serializers for limiting fields returned in responses thus limiting size and complexity of database requests.

Project description

DRF fields limiting

example workflow Coverage Status

About

This package provides utils for Django Rest Framework API views and serializers for limiting fields returned in responses thus limiting size and complexity of database requests.

Install

pip install drf-fields-limiting

Usage

Inherit from LimitedFieldsView or LimitedFieldsViewSet and configure it to enable fields limiting. View limits fields of serialized object in response with respect to Fields HTTP header.

If header is not present or is empty show all fields. Fields limiting works only in GET requests.

LIMITED_FIELDS_CONFIG holds map of field name - operation to perform on queryset (from fields_limiting.operations). It should contains entries for all fields declared in serializer_class.

...
class MySerializer(LimitedFieldsSerializer, serializers.ModelSerializer):
    
    class Meta:
        model = MyModel
        fields = (
            'char_field',
            'int_field',
            'foreign',
        )
    ...

class MyViewSet(LimitedFieldsViewSet, viewsets.ModelViewSet):
    ...
    LIMITED_FIELDS_CONFIG = {
        'char_field': Only('char_field'),
        'int_field': Only('int_field'),
        'foreign': SelectRelated('foreign'),
    }
    serializer_class = MySerializer
    ...

All available operations are in drf_fields_limiting.operations module.

Example GET query:

GET /api/v1/base/my/ HTTP/1.1
Authorization: Token h3r315myt0k3n
Accept: application/json
Content-Type: application/json
Fields: char_field,int_field

License

The DRF complete autocomplete package is licensed under the FreeBSD License.

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-fields-limiting-1.0.0.tar.gz (6.4 kB view hashes)

Uploaded Source

Built Distribution

drf_fields_limiting-1.0.0-py3-none-any.whl (9.0 kB view hashes)

Uploaded Python 3

Supported by

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