Django application to integrate pydantic models into django cache with an orm interface.
Project description
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 like django orm.
Status
Dependencies
Pydantic >= 2.6
Django >= 3.2
Install
pip install django-cache-pydantic
Usage
Edit settings.py and add django_cache_pydantic to your INSTALLED_APPS (also config CACHES setting).
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'
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
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
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
Built Distribution
Hashes for django-cache-pydantic-0.0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8652e32affb1aa2fa49b8f19f788ef142878f2a610967cc9c60fe2b345d03ccc |
|
MD5 | e193e5ef0ff4f2879321e8deafab8bf3 |
|
BLAKE2b-256 | a5a9bed84d68168a4d9335a9fd29d5017419d60bf0ba5206f2d21840141b1ccf |
Hashes for django_cache_pydantic-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f74f271e262bf4542795d9fb4bacc28f4d57ead851607116f781e369db2e445 |
|
MD5 | 3249c26d9b32d7da47c6a3cefa75d6fc |
|
BLAKE2b-256 | f0dfd67e0272123790c7a635d46cf15dabd10963bb3dc36b082d00060b2229b1 |