本文整理汇总了Python中taggit.models.TaggedItem.bulk_lookup_kwargs方法的典型用法代码示例。如果您正苦于以下问题:Python TaggedItem.bulk_lookup_kwargs方法的具体用法?Python TaggedItem.bulk_lookup_kwargs怎么用?Python TaggedItem.bulk_lookup_kwargs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类taggit.models.TaggedItem
的用法示例。
在下文中一共展示了TaggedItem.bulk_lookup_kwargs方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_tag_cloud
# 需要导入模块: from taggit.models import TaggedItem [as 别名]
# 或者: from taggit.models.TaggedItem import bulk_lookup_kwargs [as 别名]
def get_tag_cloud(model, queryset, tags_filter=None):
if tags_filter is None:
tags_filter = set()
for item in queryset.all():
tags_filter.update(item.tags.all())
tags_filter = set([tag.id for tag in tags_filter])
tags = set(TaggedItem.objects.filter(
content_type__model=model.__name__.lower()
).values_list('tag_id', flat=True))
if tags_filter is not None:
tags = tags.intersection(tags_filter)
tag_ids = list(tags)
kwargs = TaggedItem.bulk_lookup_kwargs(queryset)
kwargs['tag_id__in'] = tag_ids
counted_tags = dict(TaggedItem.objects
.filter(**kwargs)
.values('tag')
.annotate(count=models.Count('tag'))
.values_list('tag', 'count'))
tags = TaggedItem.tag_model().objects.filter(pk__in=counted_tags.keys())
for tag in tags:
tag.count = counted_tags[tag.pk]
return sorted(tags, key=lambda x: -x.count)
示例2: get_tags
# 需要导入模块: from taggit.models import TaggedItem [as 别名]
# 或者: from taggit.models.TaggedItem import bulk_lookup_kwargs [as 别名]
def get_tags(self, entries=None, language=None):
"""Returns tags used to tag post and its count. Results are ordered by count."""
if not entries:
entries = self
if language:
entries = entries.filter_by_language(language)
entries = entries.distinct()
if not entries:
return []
kwargs = TaggedItem.bulk_lookup_kwargs(entries)
# aggregate and sort
counted_tags = dict(TaggedItem.objects
.filter(**kwargs)
.values('tag')
.annotate(count=models.Count('tag'))
.values_list('tag', 'count'))
# and finally get the results
tags = Tag.objects.filter(pk__in=counted_tags.keys())
for tag in tags:
tag.count = counted_tags[tag.pk]
return sorted(tags, key=lambda x: -x.count)
示例3: get_tags
# 需要导入模块: from taggit.models import TaggedItem [as 别名]
# 或者: from taggit.models.TaggedItem import bulk_lookup_kwargs [as 别名]
def get_tags(self, request, namespace):
"""
Get tags with articles count for given namespace string.
Return list of Tag objects ordered by custom 'num_articles' attribute.
"""
if (request and hasattr(request, 'toolbar') and
request.toolbar and request.toolbar.edit_mode):
articles = self.namespace(namespace)
else:
articles = self.published().namespace(namespace)
if not articles:
# return empty iterable early not to perform useless requests
return []
kwargs = TaggedItem.bulk_lookup_kwargs(articles)
# aggregate and sort
counted_tags = dict(TaggedItem.objects
.filter(**kwargs)
.values('tag')
.annotate(tag_count=models.Count('tag'))
.values_list('tag', 'tag_count'))
# and finally get the results
tags = Tag.objects.filter(pk__in=counted_tags.keys())
for tag in tags:
tag.num_articles = counted_tags[tag.pk]
return sorted(tags, key=attrgetter('num_articles'), reverse=True)
示例4: tag_cloud
# 需要导入模块: from taggit.models import TaggedItem [as 别名]
# 或者: from taggit.models.TaggedItem import bulk_lookup_kwargs [as 别名]
def tag_cloud(self, other_model=None, queryset=None, published=True):
from taggit.models import TaggedItem
tag_ids = self._taglist(other_model, queryset)
kwargs = {}
if published:
kwargs = TaggedItem.bulk_lookup_kwargs(self.model.objects.published())
kwargs['tag_id__in'] = tag_ids
counted_tags = dict(TaggedItem.objects
.filter(**kwargs)
.values('tag')
.annotate(count=models.Count('tag'))
.values_list('tag', 'count'))
tags = TaggedItem.tag_model().objects.filter(pk__in=counted_tags.keys())
for tag in tags:
tag.count = counted_tags[tag.pk]
return sorted(tags, key=lambda x: -x.count)