Skip to main content

A Django REST Framework package that provides a flexible way to manage different serializers and paginators in your ViewSets based on defined scenarios.

Project description

ViewSet Scenarios

An elegant DRF library for managing and transforming serializers and paginators in your ViewSets based on defined scenarios

License Last commit PyPI Version Python Versions

🚀 Description

A Django REST Framework package that provides a flexible way to manage different serializers and paginators in your ViewSets based on defined scenarios.

✨ Key Features

ViewSet Scenarios allows you to:

  • Define multiple scenarios for a single ViewSet
  • Dynamically assign serializers based on actions or conditions
  • Configure pagination settings per scenario
  • Handle custom endpoints with specific serialization needs

🛠️ Installation

pip install drf-viewset-scenarios

Quick Start

Here's a simple example of how to use ViewSet Scenarios:

from viewset_scenarios import ViewSetScenarios, DRFScenario, DRFActionType
from rest_framework import viewsets

class ProductViewSet(viewsets.ModelViewSet):
    queryset = Product.objects.all()
    scenarios = ViewSetScenarios([
        DRFScenario(
            action=DRFActionType.LIST, 
            serializer=ProductListSerializer,
            pagination=StandardPagination
        ),
        DRFScenario(
            action=DRFActionType.RETRIEVE,
            serializer=ProductDetailSerializer
        ),
        DRFScenario(
            action=DRFActionType.DEFAULT,
            serializer=ProductBaseSerializer
        ),
    ])

    def get_serializer_class(self):
        return self.scenarios.get_serializer_class(self)

    @property
    def paginator(self):
        return self.scenarios.get_paginator(self)

Features

Action Types

Built-in support for common ViewSet actions:

  • LIST: List view
  • RETRIEVE: Detail view
  • CREATE: Creation view
  • UPDATE: Update view
  • DATATABLE: Special handling for datatable requests
  • DEFAULT: Fallback scenario

Scenario Configuration

Each scenario can define:

  • action: The ViewSet action or custom endpoint
  • serializer: The serializer class to use
  • pagination: Optional pagination class
  • condition: Optional condition for more complex logic

Custom Endpoints

Support for custom actions with specific serialization needs:

class ProductViewSet(ViewSetScenarios, viewsets.ModelViewSet):
    scenarios = [
        DRFScenario(
            action="featured_products",
            serializer=FeaturedProductSerializer,
            pagination=None
        ),
    ]

    @action(detail=False, methods=["get"])
    def featured_products(self, request):
        products = self.get_queryset().filter(featured=True)
        serializer = self.get_serializer(products, many=True)
        return Response(serializer.data)

Advanced Usage

Conditional Scenarios

You can use conditions to determine which serializer to use:

class OrderViewSet(viewsets.ModelViewSet):
    scenarios = ViewSetScenarios([
        DRFScenario(
            action=DRFActionType.RETRIEVE,
            serializer=OrderDetailSerializer,
            condition=lambda request: request.user.is_staff
        ),
        DRFScenario(
            action=DRFActionType.RETRIEVE,
            serializer=OrderBasicSerializer
        ),
    ])

Custom Pagination

Configure different pagination per scenario:

class ProductViewSet(viewsets.ModelViewSet):
    scenarios = ViewSetScenarios([
        DRFScenario(
            action=DRFActionType.LIST,
            serializer=ProductListSerializer,
            pagination=LargeResultsPagination
        ),
        DRFScenario(
            action="featured",
            serializer=ProductListSerializer,
            pagination=None  # Disable pagination for this endpoint
        ),
    ])

📁 Project Structure

└── viewset_scenarios/
    ├── LICENSE
    ├── pyproject.toml
    ├── README.md
    └── src/
        └── viewset_scenarios/
            ├── __init__.py
            ├── action_type.py
            └── scenarios.py

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

📫 Contact


Developed with ❤️ by Jancel

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

drf_viewset_scenarios-1.0.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_viewset_scenarios-1.0.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file drf_viewset_scenarios-1.0.0.tar.gz.

File metadata

  • Download URL: drf_viewset_scenarios-1.0.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.22.3 CPython/3.13.1 Linux/6.12.6-200.fc41.x86_64

File hashes

Hashes for drf_viewset_scenarios-1.0.0.tar.gz
Algorithm Hash digest
SHA256 305da7e8bf951f006a886faafff4ef2a8276623cb680f183464e71ba525a47d2
MD5 b33eca41671cc6b98a953dcc5b0eb40b
BLAKE2b-256 20a42a897b146e901443d9cbeba71ce5b6884b10009f9c2128a56f0223a30bd8

See more details on using hashes here.

File details

Details for the file drf_viewset_scenarios-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: drf_viewset_scenarios-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.22.3 CPython/3.13.1 Linux/6.12.6-200.fc41.x86_64

File hashes

Hashes for drf_viewset_scenarios-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 234aea0db5d50b6468f3ed294f6579aeb3f85bd0321ceb5009588768e98ff7da
MD5 18fbffb17efe57b411f4b99342f9887e
BLAKE2b-256 126d3fcffbd0a5707ec9ae61c92a4d5f24771f51649eb047e8a2afa7ed9a8c80

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page