A cacheable Django model
Project description
django cacheable model
A cacheable model for django.
- A generic way of creating cache keys from Django model fields
- Retrieve django models from cache with field values (cache on the way if cache missed)
- Retrieve all the model instances (suitable for small set of models)
See usage example below
1. Github
https://github.com/harisankar-krishna-swamy/django_cacheable_model
2. Install
pip install django_cacheable_model
3. Usage
See samples in example_django_project
views.py and models.py.
3.1. Create a model that inherits from CacheableModel
class Question(CacheableModel):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(CacheableModel):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
3.2. Use cache operations from django_cacheable_model.utils
from django_cacheable_model.utils import all_ins_from_cache, model_ins_from_cache
# Get all instances of model from cache (use for smaller set of models)
context['choices'] = all_ins_from_cache(Choice)
# Get all instances with select_related and order_by
choices = all_ins_from_cache(Choice,
select_related=('question',),
order_by_fields=('-id',))
# Get a single model
context['choice'] = model_ins_from_cache(Choice, {'id': 5})[-1]
4. To do
a) Add more tests
b) Document use of prefetch_related
5. License
Apache2 License
Project details
Release history Release notifications | RSS feed
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
Close
Hashes for django_cacheable_model-1.0.1b0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39aa80d536e6f1dbd44eefc5e16cba508c0bcb3adc0ccfea929d35ba755bb172 |
|
MD5 | 796a2cd2b763506c5e4b52eea1f41fe3 |
|
BLAKE2b-256 | 016f7fafc97b6c84337f9ce3dcedb968fce44e9dd982a0406dec035398db584f |
Close
Hashes for django_cacheable_model-1.0.1b0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6fb9ef40690b37857cfde8c99908f3067148234948a8062caa1aeaff35082cbd |
|
MD5 | 717bbd5f2291f8a7eac68494801e0036 |
|
BLAKE2b-256 | 9c0ffbaffa0b735ecb69c0594474310a816595dab4c69a660fdbecdec9ea5fd5 |