Optimized includable serializer fields.
Project description
Django REST framework light includable serializer fields
Requirements
- Python (2.7, 3.4, 3.5, 3.6)
- Django REST Framework (>=3.0)
Usage
serializers.py
class GroupSerializer(SerializerIncludeMixin, ModelSerializer):
@classproperty # django.utils.decorators
def extra_objects(cls):
return {
'users': UserSerializer(source='user_set', many=True),
'active_users': UserSerializer(many=True),
}
class Meta:
model = Group
fields = ('id', 'name')
views.py
class GroupViewSet(QueryOptimizerMixin, ReadOnlyModelViewSet):
"""
Groups.
list:
Available includable objects:
* users - all users;
* active_users - only active users.
"""
queryset = Group.objects.all()
serializer_class = GroupSerializer
select_related = {}
prefetch_related = {
'users': 'user_set',
'active_users': Prefetch(
lookup='user_set',
queryset=User.objects.filter(is_active=True),
to_attr='active_users',
),
}
And now we can to do that:
client = APIClient()
response = client.get( # GET /groups/?include[]=active_users
'/groups/',
data=[
('include[]', 'active_users'),
],
)
This will make only 2 query:
- all groups
- prefetch only active users
Documentation
rest_framework_include_mixin.SerializerIncludeMixin
Used in any serializer to automatically replace and add serializer fields.
extra_objects class variable
key:str - value from GET parameter include[].
For example, profile for /users/?include[]=profile.
value:Serializer - any field serializer.
Example:
class UserSerializer(SerializerIncludeMixin, ModelSerializer):
extra_objects = {
'profile': ProfileSerializer(),
'groups': GroupSerializer(many=True),
}
class Meta:
model = User
fields = ('id', 'profile_id')
| GET parameters | Result serializer fields |
|---|---|
| 'id': IntegerField(), 'profile_id': IntegerField() | |
| ?include[]=profile | 'id': IntegerField(), 'profile': ProfileSerializer() |
| ?include[]=groups | 'id': IntegerField(), 'profile_id': IntegerField(), 'groups': GroupSerializer(many=True) |
| ?include[]=profile&include[]=groups | 'id': IntegerField(), 'profile': ProfileSerializer(), 'groups': GroupSerializer(many=True) |
Note: standard serializer fields with/without _id will be replaced to field from extra_objects.
For ?include[]=profile: profile and profile_id will be replaced to profile from extra_objects.
rest_framework_include_mixin.QueryOptimizerMixin
Used with ModelViewSet to optimize database queries.
select_related class variable
key:str - value from GET parameter include[].
value:str - field name that can be passed to select_related (model manager function).
prefetch_related class variable
key:str - value from GET parameter include[].
value:Union[str, Prefetch] - field name or Prefetch object that can be passed to prefetch_related (model manager function).
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file djangorestframework-include-mixin-0.4.tar.gz.
File metadata
- Download URL: djangorestframework-include-mixin-0.4.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a71d29fe9fdaf8a2dcd8ed4b9caeb1014d3daff8c9f6639935e74c5c1d0c1d8c
|
|
| MD5 |
0475770591d655c495a801b22d2c0b00
|
|
| BLAKE2b-256 |
32fa3e6135561803d87c1cb26ae8aff8b0d61bf0d8daf739b9011079a4b0cf68
|
File details
Details for the file djangorestframework_include_mixin-0.4-py2.py3-none-any.whl.
File metadata
- Download URL: djangorestframework_include_mixin-0.4-py2.py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb4f0a57ee523e5f1f6dcd049ae8db838b63fdf03670720b1a4bc43e585b8204
|
|
| MD5 |
aa6e7a7142b1240d53fceed02279d128
|
|
| BLAKE2b-256 |
cad75ed591654185b0683b9bfc8436997d532276dcd3eff0e9de02a68d5d7bad
|