我已经将memcached作为我在django项目上的缓存后端。运行python manage.py test
时,出现以下错误:
AttributeError: type object 'FakeModel' has no attribute '_deferred'
详细日志如下:
File "/home/my_project/virtualenv_my_project/local/lib/python2.7/site-packages/django/core/cache/backends/memcached.py", line 82, in set
self._cache.set(key, value, self._get_memcache_timeout(timeout))
File "/home/my_project/virtualenv_my_project/local/lib/python2.7/site-packages/memcache.py", line 584, in set
return self._set("set", key, val, time, min_compress_len)
File "/home/my_project/virtualenv_my_project/local/lib/python2.7/site-packages/memcache.py", line 835, in _set
return _unsafe_set()
File "/home/my_project/virtualenv_my_project/local/lib/python2.7/site-packages/memcache.py", line 812, in _unsafe_set
store_info = self._val_to_store_info(val, min_compress_len)
File "/home/my_project/virtualenv_my_project/local/lib/python2.7/site-packages/memcache.py", line 782, in _val_to_store_info
pickler.dump(val)
File "/home/my_project/virtualenv_my_project/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 186, in __reduce__
if self.model._deferred:
AttributeError: type object 'FakeModel' has no attribute '_deferred'
每当缓存超时并且需要在缓存中存储某些内容时,就会发生错误。如果我运行我的开发服务器并再次运行测试,则会显示错误。
更新
Memcached设置:
CACHES = {
'default': {
'BACKEND':'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION':'127.0.0.1:11211',
}
}
我用于缓存的一件事是查询集返回的输出:
top_students = Student.objects.filter(.....).filter(
..........
).annotate(
.........
).all().order_by(........)[:3]
学生模型是这样的:
class Student(models.Model):
user = models.ForeignKey(User, unique=True, null=True,
blank=True, related_name="student")
description = models.CharField(max_length=1000, null=True, blank=True)
rollno = models.CharField(max_length=20)
phoneno = models.CharField(max_length=15, null=True, blank=True)
enrollYear = models.ForeignKey(EnrollYear, null=True)
college = models.ForeignKey(College, null=True, blank=True)
faculty = models.ForeignKey(Faculty, null=True, blank=True)
verification = models.ManyToManyField(VerifyStudent)