本文整理匯總了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())