Skip to main content

Improved interaction with DRF relations.

Project description

AIR-DRF-RELATION

Table of Contents

  1. Instalation
  2. About
  3. RelatedField
    1. pk_only
    2. hidden
  4. AirModelSerializer
    1. user - docs coming soon
    2. extra_kwargs - docs coming soon
    3. action_read_only_fields - docs coming soon
    4. action_hidden_fields - docs ccming soon
    5. action_extra_kwargs - docs coming soon
    6. queryset_related_field - docs coming soon
    7. nested_save_fields_fields - docs coming soon

Instalation

$ pip install air-drf-relation

About

Add folder models in src. Add a file to the directory that will contain the model, for example user.

RelatedField

Used to extend the functionality of the PrimaryKeyRelatedField

class BookSerializer(ModelSerializer):
    # author = PrimaryKeyRelatedField(queryset=Author.objects) - default usage
    author = RelatedField(AuthorSerializer)
    city = RelatedField(AuthorSerializer)

    class Meta:
        model = Book
        fields = ('uuid', 'name', 'author', 'city')

RelatedField allows you to get not only pk but also an object with pk, which will be searched.

{
    "name": "demo",
    "author": { 
        "id": 1
    },
    "city": 1
}

pk_only

Automatically RelatedField returns a serialized object. If you only want to use pk, you must specify the pk_only key.

author = RelatedField(AuthorSerializer, pk_only=True)

hidden

Hidden fields are not used for serialization and validation. The data will be returned without fields. Usually used together in AirModelSerializer

author = RelatedField(AuthorSerializer, hidden=True)

extra_kwargs

You cannot use hidden and pk_only in ModelSerializer and with extra_kwargs

AirModelSerializer

Used to extend the functionality of the ModelSerializer

class BookSerializer(AirModelSerializer): # full example
    author = RelatedField(AuthorSerializer)
    city = RelatedField(AuthorSerializer)

    class Meta:
        model = Book
        fields = ('uuid', 'name', 'author', 'city')
        hidden_fields = () # list of hidden fields
        read_only_fields = () # default read_only_fields
        extra_kwargs = {} # default extra_kwargs with support custom keys
        action_read_only_fields = { # set read_only_fields by action
            'create': {},
            '_': {} # used for other actions
        },
        action_hidden_fields = { # set hidden_fields by action
            'create': (),
            '_': () # used for other actions
        }
        action_extra_kwargs = { # set extra_kwargs by action
            'list': {},
            '_': {} # used for other actions
        }
        nested_save_fields = () # used for save nested objects
        

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

air_drf_relation-0.0.19.tar.gz (18.3 kB view hashes)

Uploaded Source

Built Distribution

air_drf_relation-0.0.19-py3-none-any.whl (26.9 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