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 Distributions
Hashes for djangorestframework-include-mixin-0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1eef6538af27b1ff9759fd81279c3388852da274c1fa093d4c4faf86bb349fee |
|
MD5 | 76b601d9fe5142ee77886fff9fed90f5 |
|
BLAKE2b-256 | 713929316ba788897be3ccfe1ab4a515079a2391295a7b3e3bea9faf9f40ca1b |
Hashes for djangorestframework_include_mixin-0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ed356c314419c61c5559cb2550279453e122a7a14b29cdcee482acfc0e1dda8 |
|
MD5 | 8cec4719b5f10bc276483f78a24c70ad |
|
BLAKE2b-256 | cfd7373694e5ed2e386aeda737fa3aaacca3e4be8afc85d621190981e7f76d25 |
Hashes for djangorestframework_include_mixin-0.3-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24ad0c5b26f8a5f25435a8a8adbf6956cc483209b2f73f815adcdf88b390b578 |
|
MD5 | 2a69b83d399a345f4619574b98e733dc |
|
BLAKE2b-256 | cd381d1568409814f50116b3c544e8d02f5a9ca558607440b07ce51b6970de23 |