Search code examples
pythondjangodjango-ormdjango-cache-machine

Bug in Django CacheMachine ignores saving of Model?


I have a very simple Django class:

from django.db import models

class MyClass(models.Model):
    a = models.IntegerField()
    b = models.IntegerField()

    def __str__(self):
        return "MyClass #%s: a: %s, b %s" % (self.pk, self.a, self.b)

This class works fine when I perform CRUD-type operations:

>>> from statistics.models import MyClass
>>> print MyClass.objects.all()
[]
>>> x = MyClass(a=6, b=10)
>>> print MyClass.objects.all()
[]
>>> print x
MyClass #None: a: 6, b 10
>>> x.save()
>>> print MyClass.objects.all()
[<MyClass: MyClass #2: a: 6, b 10>]
>>>

Now I add/modify 3 lines to the class definition to enable the CacheMachine:

from caching.base import CachingManager, CachingMixin  # This line added
from django.db import models

class MyClass(CachingMixin, models.Model):             # Added a Mix-in
    a = models.IntegerField()
    b = models.IntegerField()
    objects = CachingManager()                         # This line added

    def __str__(self):
        return "MyClass #%s: a: %s, b %s" % (self.pk, self.a, self.b)

After manage.py makemigrations and manage.py migrate, I truncate the database, flush memcached and run the same experiment I ran before.

However that experiment fails! After saving the newly created MyClass instance, querying the database shows it is not there. Why? How to workaround this issue? Has anyone else seen this?

>>> print MyClass.objects.all()
[]
>>> x = MyClass(a=6, b=10)
>>> print MyClass.objects.all()
[]
>>> print x
MyClass #None: a: 6, b 10
>>> x.save()
>>> print MyClass.objects.all()
[]

FYI, I ran the tests for Django Cache Machine shown here. They all passed.


Solution

  • I've just tested my fork on a windows machine which I was now an check yourself:

    Image check: http://oi61.tinypic.com/2w5jf9d.jpg

    Github URL: https://github.com/asketsus/django-cache-machine