Skip to main content

Optimized includable serializer fields.

Project description

Django REST framework light includable serializer fields

Requirements

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djangorestframework-include-mixin-0.4.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

djangorestframework_include_mixin-0.4-py2.py3-none-any.whl (5.1 kB view hashes)

Uploaded Python 2 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