0
객체와 관련된 주석을 가져올 수 있습니까? 지금 django 주석 프레임 워크는 관련 주석이있는 모든 객체와 주석 소유자에 대한 다른 쿼리에 대한 쿼리를 작성합니다. 나는 이것을 어떻게 든 피할 수 있습니까? 장고 1.4를 사용하므로 prefetch_related가 허용됩니다.하나의 쿼리를 사용하여 객체에 대한 의견을 얻으십시오.
from django.contrib.contenttypes.models import ContentType
from django.contrib import comments
def get_comment_count_key(model):
content_type = ContentType.objects.get_for_model(model)
return 'comment_count_%s_%s' % (content_type.pk, model.pk)
def get_comment_count(model):
key = get_comment_count_key(model)
value = cache.get(key)
if value is None:
value = comments.get_model().objects.filter(
content_type = ContentType.objects.get_for_model(model),
object_pk = model.pk,
site__pk = settings.SITE_ID
).count()
cache.set(key, value)
return value
당신 수 extend the Comment model을 거기 get_comment_count을 추가
댓글 프레임 워크는 일반적인 외래 키를 사용하여 설명을 객체와 연결합니다. 정의에 따르면, ContentType을 검색하고 실제 객체를 가져 오는 데 사용해야하기 때문에 다중 쿼리가 필요합니다. –