本文整理汇总了Python中models.Tag.get_or_create方法的典型用法代码示例。如果您正苦于以下问题:Python Tag.get_or_create方法的具体用法?Python Tag.get_or_create怎么用?Python Tag.get_or_create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Tag
的用法示例。
在下文中一共展示了Tag.get_or_create方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: log_hours
# 需要导入模块: from models import Tag [as 别名]
# 或者: from models.Tag import get_or_create [as 别名]
def log_hours():
logging.basicConfig(level=logging.INFO)
categories = settings.HAMSTER_TO_DP.keys()
tag_logged = Tag.get_or_create(name = '_logged_in_dp_')
already_tagged = [ft.fact.id for ft in FactTag.filter(tag=tag_logged).distinct()]
#get all facts that belong to exportable categories, finished, and
# not previously posted
facts = Fact.filter(
Q(activity__category__name__in=categories) &
~Q(end_time__is=None) &
# ~Q(fact_tags_set__tag=tag_logged) # * see note
~Q(id__in=already_tagged)
)
# NOTE
# I want to exclude Facts tagged with ``tag_logged``, . but that ~Q() condition
# only exclude the facts that are ONLY tagged with ``tag_logged``.
# the last Q is a workaround but imply a nested select.
# How I should write this query?
if not facts.exists():
logging.info("You're up to date! There is no unsynced tasks")
return
br = DotProjectBot(settings.DP_BASE_URL)
br.login(settings.DP_USERNAME, settings.DP_PASSWORD)
for f in facts:
#process data
tags = ', '.join([ft.tag.name for ft in f.fact_tags_set])
if tags and f.description:
description = '%s %s: %s' % (f.activity.name, tags, f.description)
elif tags:
description = '%s %s' % (f.activity.name, tags)
elif f.description:
description = '%s %s' % (f.activity.name, f.description)
else:
description = f.activity.name
dp_task_id = settings.HAMSTER_TO_DP[f.category.name]
#and post the fact into dotproject!
br.log_task(dp_task_id, f.start_time, f.duration, description)
#then mark the fact as logged.
ft = FactTag(fact=f, tag=tag_logged)
ft.save()
示例2: str
# 需要导入模块: from models import Tag [as 别名]
# 或者: from models.Tag import get_or_create [as 别名]
return [keyword[0].decode('utf-8') + u'__' + str(keyword[1]).decode('utf-8') for keyword in keywords]
@classmethod
def add_keyword(cls, word):
word_utf8 = to_unicode(word)
key = '%s%s' %(TAG_CACHE_KEY, word_utf8)
if not cls.is_keyword(word):
cache.incr(key, amount=1)
if __name__ == '__main__':
file = open('data/content', 'r')
content = ''.join([unicode(line, 'utf-8') for line in file.readlines()])
file.close()
keywords = WordSeg.parse(content)
originals = [keyword.split('__')[0] for keyword in keywords]
friends = []
for o in originals:
tag = Tag.get_or_create(o)
friends.extend(tag.friends)
friends = sum_list(friends)
print("Original tags")
for k in keywords:
print(k)
print("Friends tags")
for f in friends:
print(to_unicode(f[0]) + u'__' + unicode(f[1]))
示例3: _new_tag
# 需要导入模块: from models import Tag [as 别名]
# 或者: from models.Tag import get_or_create [as 别名]
def _new_tag(self, tag):
name = tag.get('term') or tag.get('label')
clean_name = self._sanitize_title(name)
tag, is_new = Tag.get_or_create(name=clean_name)
print('Saved tag "{}": new={}'.format(clean_name, is_new))
return tag