Improved interaction with DRF relations.
Project description
AIR-DRF-RELATION
Table of Contents
- Instalation
- About
- RelatedField
- AirModelSerializer
- user - docs coming soon
- extra_kwargs - docs coming soon
- action_read_only_fields - docs coming soon
- action_hidden_fields - docs ccming soon
- action_extra_kwargs - docs coming soon
- queryset_related_field - docs coming soon
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for air_drf_relation-0.0.19-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ec7573fc51c3b5fce99db21c6d42f37e5184376689f9d6d93c6fd805046bcda |
|
MD5 | 73c17b03dfa0cbd3e8147fa01fc1863b |
|
BLAKE2b-256 | c3b353b9d2ff4cca3b6430773b6def57c5f43a2c6d6fa35dc6be212536645513 |