Git Product home page Git Product logo

django-model-cache's Introduction

django-model-cache

Build Status

An easy-to-use cache for Django models.

This code has been developed and used in a production environment for one year.

How to use

There follows some examples of use. For further examples, see tests/simple/tests.py.

from django.db import models
from django_model_cache import CacheController
import uuid


class Brand(models.Model):
    name = models.CharField(max_length=128)

    cache = CacheController(timeout=None)


class Product(models.Model):
    code = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
    brand = models.ForeignKey('Brand')
    name = models.CharField(max_length=255)

    cache = CacheController(fields=['code', ('brand_id', 'name')], related_fields=['brand'], timeout=None)

    class Meta:
        unique_together = ('name', 'brand')


# Get a product by pk.
product = Product.cache.get(pk=1)

# Get a product by a unique key.
product = Product.cache.get(code='A001')

# Load the related models.
product.load_related()

How to test

Just run tox or install the dependencies and run cd tests/ && ./manage.py test.

django-model-cache's People

Contributors

patriciaborges avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

django-model-cache's Issues

Django version compatibility

I have had a bit of a play with the code trying to get it to work but on 2.1 I am getting the following error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/bla/.virtualenvs/virty/lib/python3.6/site-packages/django/db/models/manager.py", line 192, in __get__
    return cls._meta.managers_map[self.manager.name]

Even though CacheController doesn't inherit from models.Manager it appears to be treated as a manager. I tried making CacheController inherit and various variations on that but I keep getting the same error.
Could this be a Django 2.1+ thing

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.