在Django 1.9中访问模型缓存

时间:2016-04-11 09:11:46

标签: python django caching

可以使用from django.core.cache import cache访问Django1.9缓存。 但是如何访问模型缓存?

此代码适用于以前的版本。

from django.db.models.loading import cache
from django.db import models


def get_custom_car_model(car_model_definition):
  """ Create a custom (dynamic) model class based on the given definition.
  """
  # What's the name of your app?
  _app_label = 'myapp'

  # you need to come up with a unique table name
  _db_table = 'dynamic_car_%d' % car_model_definition.pk

  # you need to come up with a unique model name (used in model caching)
  _model_name = "DynamicCar%d" % car_model_definition.pk

  # Remove any exist model definition from Django's cache
  try:
    del cache.app_models[_app_label][_model_name.lower()]
  except KeyError:
    pass
...

我现在该怎么办?我希望删除模型缓存

我收到错误

'LocMemCache' object has no attribute app_models

1 个答案:

答案 0 :(得分:0)

嗨,也许是尝试/除了这种方式,您可以访问缓存并保持对以前版本的支持

try:
    from django.apps import apps as cache
except ImportError:
    from django.db.models.loading import cache