Skip to main content

Django Cache Pydantic is a minimal wrapper around django cache framework which allows you to create pydantic instances directly inside your django project cache and retrieve them using a similar interface to django orm.

Status

https://img.shields.io/github/actions/workflow/status/bindruid/django-cache-pydantic/test.yml.svg?branch=master https://img.shields.io/pypi/v/django-cache-pydantic.svg https://img.shields.io/pypi/pyversions/django-cache-pydantic.svg https://img.shields.io/pypi/djversions/django-cache-pydantic.svg

Dependencies

  • Pydantic >= 2.6

  • Django >= 3.2

Install

pip install django-cache-pydantic

Usage

  1. Edit settings.py and add django_cache_pydantic to your INSTALLED_APPS (also config CACHES setting).

  2. Inherit your pydantic model from PydanticCachedModel.

from pydantic import Field, Optional
from datetime import datetime
from django_cache_pydantic import PydanticCachedModel


class Person(PydanticCachedModel):
    national_id: str = Field(max_length=10, pattern=r'^\d*$')
    first_name: Optional[str] = None
    last_name: Optional[str] = None
    mobile_number: str = Field(max_length=11, pattern=r'^\d*$')
    created_at: datetime = Field(default_factory=lambda: datetime.now())

    class CacheMeta:
        ttl = 5 * 60
        primary_key_field = 'national_id'
  1. Create your model instance directly into the cache via calling to save method or object manager create method.

# some where in your views
person = Person(national_id='123456789', mobile_number='0930444444')
person.save()  # will save the instance into project default cache
# some where in your views
Person.objects.create(national_id='123456789', mobile_number='0930444444')  # will save the instance into project default cache
  1. Retrieve your model instance from the cache via calling to object manager get method.

# some where in your views
person = Person.objects.get(pk='123456789')
if person is not None:
    # do some stuff

Cache pydantic meta class

  • You can control cache pydantic models behavior using a custom meta class called CacheMeta.

class CacheMeta:
    cache_backend: str  # refers to a predefined cache settings
    ttl: int  # default timeout for instance to live in cache
    primary_key_field: str  # could be set to be used as cache key
    verbose: str  # verbose name of base model

Cache pydantic Project Settings

  • Default cache to save pydantic models into.

CACHE_PYDANTIC_DEFAULT_CACHE
  • Default time to live of the pydantic cached models.

CACHE_PYDANTIC_DEFAULT_TTL

Download files

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

Source Distribution

django_cache_pydantic-0.1.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

django_cache_pydantic-0.1.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file django_cache_pydantic-0.1.1.tar.gz.

File metadata

  • Download URL: django_cache_pydantic-0.1.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.4

File hashes

Hashes for django_cache_pydantic-0.1.1.tar.gz
Algorithm Hash digest
SHA256 172cc2b385311e0cfb4375643e87f567c3a2f6259b9dbd7a3012d6fca6b3a4d3
MD5 790451c5bd51d33f8319b012fa95bd7f
BLAKE2b-256 8e8fb620e6956f5eb44e63cfd1a08cf4c00935301b835a553c181792a739fa96

See more details on using hashes here.

File details

Details for the file django_cache_pydantic-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_cache_pydantic-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f32c239e7fca18290ad369c2252ee3b32fc934e57c2eb3d5fd66c3452d519a78
MD5 bd6df1c5b6f1a7ae864588e6462cf997
BLAKE2b-256 badfeb18eee23321b170f9e9e6bb09ba6b7a8be5b6a6dbb3b87091dc63f3640e

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

Supported by

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