Skip to main content

Fine tuning for serialized fields

Project description

drf-optionalfields gives you the flexibility to specify which fields you want to serialized.

There are two mixins available: OptionalFieldsMixin: Which allows you to set some optional fields. These fields can be requested via GET params DynamicFilterMixin: Which allows you to specify only the fields you need.

from rest_framework.serializers import ModelSerializer
from drf_optionalfields import OptionalFieldsMixin

class MyModelSerializer(OptionalFieldsMixin, ModelSerializer):
    class Meta:
        model = SomeModel
        fields = ("id", "username", "address", "full_name")
        optional_fields = ("address", "full_name")
# or combined with DynamicFilterMixin
class MyModelSerializer(OptionalFieldsMixin, DynamicFilterMixin, ModelSerializer):
    class Meta:
        model = SomeModel
        fields = ("id", "username", "address", "full_name")
        optional_fields = ("address", "full_name")
# You want to see an "address" you have set in the Meta:
GET /users/1/?fields=address

# You want only to return "username":
GET /users/1/?filter=username

# You want only to return the optional "full_name":
GET /users/1/?fields=full_name&filter=username

Thanks to Wim Glen for providing great bootstrap code with <https://github.com/wimglenn/djangorestframework-queryfields>(djangorestframework-queryfields)

Project details


Download files

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

Files for drf-optionalfields, version 0.9.14
Filename, size File type Python version Upload date Hashes
Filename, size drf-optionalfields-0.9.14.tar.gz (3.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page