ViewSet Scenarios
An elegant DRF library for managing and transforming serializers and paginators in your ViewSets based on defined scenarios
🚀 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 viewRETRIEVE: Detail viewCREATE: Creation viewUPDATE: Update viewDATATABLE: Special handling for datatable requestsDEFAULT: Fallback scenario
Scenario Configuration
Each scenario can define:
action: The ViewSet action or custom endpointserializer: The serializer class to usepagination: Optional pagination classcondition: 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
- GitHub: @jalvarezgom
- PyPI: drf-viewset-scenarios
Developed with ❤️ by Jancel
Metadata
Release files for drf-viewset-scenarios 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| drf_viewset_scenarios-1.0.0.tar.gz | 6.8 kB | Without provenance | Details for drf_viewset_scenarios-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| drf_viewset_scenarios-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for drf_viewset_scenarios-1.0.0-py3-none-any.whl |
Total release size: 14.3 kB
Release files / drf_viewset_scenarios-1.0.0.tar.gz
| Download URL | drf_viewset_scenarios-1.0.0.tar.gz |
|---|---|
| Size | 6.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
305da7e8bf951f006a886faafff4ef2a8276623cb680f183464e71ba525a47d2
|
|
BLAKE2b-256 checksum How to use checksums |
20a42a897b146e901443d9cbeba71ce5b6884b10009f9c2128a56f0223a30bd8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.22.3 CPython/3.13.1 Linux/6.12.6-200.fc41.x86_64
|
Release files / drf_viewset_scenarios-1.0.0-py3-none-any.whl
| Download URL | drf_viewset_scenarios-1.0.0-py3-none-any.whl |
|---|---|
| Size | 7.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
234aea0db5d50b6468f3ed294f6579aeb3f85bd0321ceb5009588768e98ff7da
|
|
BLAKE2b-256 checksum How to use checksums |
126d3fcffbd0a5707ec9ae61c92a4d5f24771f51649eb047e8a2afa7ed9a8c80
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.22.3 CPython/3.13.1 Linux/6.12.6-200.fc41.x86_64
|
Security for drf-viewset-scenarios 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.