restricted_fields package

Submodules

restricted_fields.mixins module

class restricted_fields.mixins.RestrictedFieldsSerializerMixin[source]

Bases: object

API Serializer mixin

This provides support for restricting serialized data to only a subset of fields using the only and defer query parameters.

Parameters
  • only (list) – Restricted to only a subset of fields (Include these fields).

  • defer (list) – Defer the listed fields (Exclude these fields).

Example:

Serialize only the id and name fields.

GET https://.../api/users/?only=id&only=name
{
    "count":1,
    "next": "http://127.0.0.1:8000/api/users/?only=id&only=name&page=1",
    "previous":null,
    "results":[
        {
            "id":1,
            "name": "Test"
        },
        ...
    ],
}
DEFERRED_FIELDS_PARAM = 'defer'
RESTRICTED_FIELDS_PARAM = 'only'
property context
to_representation(instance)[source]

Convert Model Object instance -> Dict of primitive datatypes.

Parameters

instance (django.db.models.Model) – The django model instance.

Returns

Dictionary of fields and corresponding value.

Module contents

Top-level package for drf-restricted-fields.

class restricted_fields.RestrictedFieldsSerializerMixin[source]

Bases: object

API Serializer mixin

This provides support for restricting serialized data to only a subset of fields using the only and defer query parameters.

Parameters
  • only (list) – Restricted to only a subset of fields (Include these fields).

  • defer (list) – Defer the listed fields (Exclude these fields).

Example:

Serialize only the id and name fields.

GET https://.../api/users/?only=id&only=name
{
    "count":1,
    "next": "http://127.0.0.1:8000/api/users/?only=id&only=name&page=1",
    "previous":null,
    "results":[
        {
            "id":1,
            "name": "Test"
        },
        ...
    ],
}
DEFERRED_FIELDS_PARAM = 'defer'
RESTRICTED_FIELDS_PARAM = 'only'
property context
to_representation(instance)[source]

Convert Model Object instance -> Dict of primitive datatypes.

Parameters

instance (django.db.models.Model) – The django model instance.

Returns

Dictionary of fields and corresponding value.