本文整理汇总了Python中django.contrib.contenttypes.models.ContentType.add_to_class方法的典型用法代码示例。如果您正苦于以下问题:Python ContentType.add_to_class方法的具体用法?Python ContentType.add_to_class怎么用?Python ContentType.add_to_class使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.contenttypes.models.ContentType
的用法示例。
在下文中一共展示了ContentType.add_to_class方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ThreadedCommentCacheManager
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import add_to_class [as 别名]
#-*- coding: utf-8 -*-
from cache_manager import CacheManager
from django.contrib.contenttypes.models import ContentType
from keys import KEYS
class ThreadedCommentCacheManager(CacheManager):
KEYS = KEYS
def comments_for_object(self, query, **kwargs):
key = self.make_key("comments_for_object", **query)
queryset = self.get_manager().filter(is_public=True, is_removed=False, **query)
return self._filter(key, queryset, **kwargs)
def update(self, instance, **kwargs):
content_type = ContentType.objects.get_for_model(instance.content_object)
self.comments_for_object(query={"content_type__id":content_type.id,
"object_pk": instance.content_object.id
}, clear=True)
class ContentTypeCacheManager(CacheManager):
KEYS = KEYS
def get_for_model(self, model, **kwargs):
opts = self.get_manager()._get_opts(model)
query = {"app_label": opts.app_label,
"model": opts.object_name.lower()}
key = self.make_key("contenttype_for_object", **query)
return self._get(key, query=query, **kwargs)
ContentType.add_to_class('cache', ContentTypeCacheManager())