A django-restframework extension to dynamically specify the returned field.
Project description
drf-optional-fields
A django-restframework extension to dynamically specify the returned field.
Requirements
- Python: 3.6, 3.7
- Django: 2.0, 2.1, 2.2
- DRF: 3.9
Installation
Install using pip:
pip install django-queryset-exts
pip install drf-optional-fields
Example:
Use fields
in query parameters to specify the returned field.
For example, fields=id,name,info{city{name},location}
will return a dict like below:
{
"id": "id",
"name": "A",
"info": {
"city": {
"name": "city name"
},
"location": "localtion"
}
}
Obviously, it refers to facebook field
Usage
First, make your serializer class inherit from drf_optional_fields.serializers.OptionalFieldsMixin
from django.db.models.query import Prefetch
from rest_framework import serializers
from django_queryset_exts.query import SelectAPIRelated
from drf_optional_fields.serializers import OptionalFieldsMixin
class MyModelsOptionalFieldsSerializer(OptionalFieldsMixin, serializers.ModelSerializer):
class Meta:
models = MyModel
fields = ('field1', 'field2', 'foreign_key_field', 'reverse_many_to_one_field', 'remote_uuid_field')
# use those configs to reduce queries to db
fields_related_query = {
'foreign_key_field': ('foreign_key_field',), # use foreign key field name directly
'remote_uuid_field': (SelectAPIRelated('remote_uuid_field'),),
'reverse_many_to_one_field': (Prefetch('reverse_many_to_one_field'))
}
default_fields = deepcopy(fields) # change this line to specify default fields, for example: default_fields = ('field1', )
Then, make your api view inherit from drf_optional_fields.views.OptionalFieldViewMixin
from rest_framework.generics import ListAPIView
from drf_optional_fields.views import OptionalFieldViewMixin
class MyModelListView(OptionalFieldViewMixin, ListAPIView):
queryset = MyModel.objects.filter(is_deleted=False)
serializer_class = MyModelsOptionalFieldsSerializer
It's done.
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
File details
Details for the file drf-optional-fields-0.0.1.tar.gz
.
File metadata
- Download URL: drf-optional-fields-0.0.1.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63045b033b056157e713a3c11b585f793d1b8c337da34b51dfbae5f6735c97fe |
|
MD5 | 62aaecee68c242e99c5ef34389c973f1 |
|
BLAKE2b-256 | 94df70c5e62b94093b55ebe06da9ef85b2c77fdd7ddd5c2b5694ddc95f10f24f |
File details
Details for the file drf_optional_fields-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: drf_optional_fields-0.0.1-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecc2da565d3fa7a26d86f78ab8d9f3fabe9a43d61001ba752dccd84b1430ac17 |
|
MD5 | dad2848431482abaa85f6f105d511232 |
|
BLAKE2b-256 | 8b976daf4d26f983cd1f11ab316e7f18b3c3c270e162e84b62f8724d5176b420 |