DRF Queryset Mixin
Project description
Introduction
The DRF Queryset Mixin is a utility for optimizing database queries in Django REST Framework (DRF) viewsets. It provides a mixin called QuerysetMixin
that allows you to easily optimize your queries by specifying deferred, selected related, and prefetched related fields.
Installation
pip install drf-queryset
Usage
Please follow the steps:
- Import the
QuerysetMixin
fromdrf_queryset.mixins
. - In your viewset class, inherit from
QuerysetMixin
. only_fields
: List of fields to be only fetched in the query.defer_fields
: List of fields to be deferred in the query.select_related_fields
: List of fields to be select_related in the query.prefetch_related_fields
: List of fields to be prefetch_related in the query.
Note: These fields are optional, you can use them depending upon your requirements of queryset and structure of the model fields.
Example
Here's a basic example of how you can use this package in your django rest framework application:
from drf_queryset.mixins import QuerysetMixin
from rest_framework.viewsets import ModelViewSet
from .models import YourModel
from .serializers import YourSerializer
class MyViewSet(QuerysetMixin, ModelViewSet):
queryset = YourModel.objects.all() # Replace with your queryset
serializer_class = YourSerializer # Replace with your serializer
defer_fields = ['field1', 'field2'] # Add your defer fields here
only_fields = ['field3', 'field4'] # Add your only fields here
select_related_fields = ['related_field1', 'related_field2'] # Add your select_related fields here
prefetch_related_fields = ['m2m_field', 'reverse_fk_field'] # Add your prefetch_related fields here
License
MIT License
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
drf-queryset-0.1.0.tar.gz
(3.0 kB
view hashes)
Built Distribution
Close
Hashes for drf_queryset-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5265ebb7456433ac77a57ba86e29d8ba42c94a9da63842229ac0d47534e1c51 |
|
MD5 | 2bdb3c5465755956ae0c1d4f930b3cd7 |
|
BLAKE2b-256 | 1e99d1719418ea5603e8d01aff4001bd19e0302e5e0cab03a7c8fb50f2a73ae1 |