Skip to main content

A Django utility suite with additional features

Project description

Django Utility Suite

Description

django_utility_suite is a set of utilities to enhance Django development

Installation

You can install this package with:

pip install django-utility-suite

Or if you use Poetry:

poetry add django-utility-suite

Usage

Django Configuration

Add django_utility_suite to INSTALLED_APPS in your settings.py:

INSTALLED_APPS = [
    ...
    'django_utility_suite',
]

Package utils

1. Performance optimization and cache management.

Using ReadOnlyViewSetMixin

This mixin provides an optimized way to handle caching in read-only views.

from django_utility_suite.api.mixins import ReadOnlyViewSetMixin
from rest_framework.viewsets import ReadOnlyModelViewSet
from .models import MyModel
from .serializers import MyModelSerializer

class MyModelViewSet(ReadOnlyViewSetMixin, ReadOnlyModelViewSet):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer
    
    def get_cache_key(self):
        if self.action == "retrieve":
            return f"mimodel-retrieve-{self.lookup_field}"
        elif self.action == "list":
            return f"mimodel-list"
    
    @property
    def cache_prefix(self):
        return "my_model"

Using create_key_from_list (minimal if/else example)

Use create_key_from_list to cache responses under a short key generated from a hash of the list (normalized, regardless of order or duplicates). This avoids very long keys and gives deterministic cache hits; you can adjust the length with length_bytes.

Example: ["A", "b", "a"]my_model:{list}:Q2r8y8tP (same hash as ["b","a"]).

from django_utility_suite.api.mixins import ReadOnlyViewSetMixin
from rest_framework.viewsets import ReadOnlyModelViewSet
from .models import MyModel
from .serializers import MyModelSerializer

class MyModelViewSet(ReadOnlyViewSetMixin, ReadOnlyModelViewSet):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer

    def get_cache_key(self):
        params = self.request.query_params
        data = getattr(self.request, "data", {})
        lookup_value = self.kwargs.get(self.lookup_field, "")

        if self.action == "retrieve":
            # Simple scalar key for detail
            return lookup_value

        elif self.action == "list":
            # Example: combine a scalar filter with a list-type query param (?slugs=a,b,c)
            category = params.get("category", "")
            slugs = (params.get("slugs") or "").split(",")
            # Use an empty prefix so the mixin's cache_prefix is the only namespace
            return f"list:{category}:{self.create_key_from_list(slugs, length_bytes=10, prefix='')}"

        elif self.action == "search":
            # Example: list-type input coming from JSON payload: {"ids": [1,2,3]}
            ids = data.get("ids", [])
            return f"search:{self.create_key_from_list(ids, length_bytes=10, prefix='')}"

        # Default (no cache or unsupported action)
        return ""

    @property
    def cache_prefix(self):
        # Global namespace for this viewset in the cache backend
        return "my_model"

Development

If you want to contribute, clone the repository and use Poetry to manage dependencies:

git clone https://github.com/yourusername/django-utility-suite.git
cd django-utility-suite
poetry install

License

This project is licensed under the MIT license.

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

django_utility_suite-0.1.8.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

django_utility_suite-0.1.8-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file django_utility_suite-0.1.8.tar.gz.

File metadata

  • Download URL: django_utility_suite-0.1.8.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.0 CPython/3.11.13 Linux/5.15.154+

File hashes

Hashes for django_utility_suite-0.1.8.tar.gz
Algorithm Hash digest
SHA256 1bf5eb2bedc89f50ea47eaf42e9080c7513fe120c03d8c849e1bcb1cc4e2a672
MD5 cb5e5173c4b4a0c811c88035e6aba5a1
BLAKE2b-256 235deec6bd1f312e9ae1f162b17613e55f153e96fd7b693bd8496afa8736c1fd

See more details on using hashes here.

File details

Details for the file django_utility_suite-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for django_utility_suite-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 fc12ddf095815cf73ce14ed487bf62d3d3dd54eee8ca945e6499846480b1dc28
MD5 7c4197f5ef7d4440b004a1c3b8630a2f
BLAKE2b-256 7d5a2d3fe1c1337204849940aaa00650fa732d4940702dd4f3cb07e098f24bfa

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