本文整理汇总了Python中taggit.models.Tag方法的典型用法代码示例。如果您正苦于以下问题:Python models.Tag方法的具体用法?Python models.Tag怎么用?Python models.Tag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类taggit.models
的用法示例。
在下文中一共展示了models.Tag方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post_list
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def post_list(request, tag_slug=None):
object_list = Post.published.all()
tag = None
if tag_slug:
tag = get_object_or_404(Tag, slug=tag_slug)
object_list = object_list.filter(tags__in=[tag])
paginator = Paginator(object_list, 3) # 3 posts in each page
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer deliver the first page
posts = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
posts = paginator.page(paginator.num_pages)
return render(request,
'blog/post/list.html',
{'page': page,
'posts': posts,
'tag': tag})
示例2: test_tag_form_field
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def test_tag_form_field(self):
class PlaceForm(ClusterForm):
class Meta:
model = Place
exclude_formsets = ['tagged_items', 'reviews']
fields = ['name', 'tags']
mission_burrito = Place(name='Mission Burrito')
mission_burrito.tags.add('mexican', 'burrito')
form = PlaceForm(instance=mission_burrito)
self.assertEqual(2, len(form['tags'].value()))
expected_instance = TaggedPlace if TAGGIT_VERSION < (1,) else Tag
self.assertEqual(expected_instance, form['tags'].value()[0].__class__)
form = PlaceForm({
'name': "Mission Burrito",
'tags': "burrito, fajita"
}, instance=mission_burrito)
self.assertTrue(form.is_valid())
mission_burrito = form.save(commit=False)
self.assertTrue(Tag.objects.get(name='burrito') in mission_burrito.tags.all())
self.assertTrue(Tag.objects.get(name='fajita') in mission_burrito.tags.all())
self.assertFalse(Tag.objects.get(name='mexican') in mission_burrito.tags.all())
示例3: test_create_with_tags
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def test_create_with_tags(self):
class PlaceForm(ClusterForm):
class Meta:
model = Place
exclude_formsets = ['tagged_items', 'reviews']
fields = ['name', 'tags']
form = PlaceForm({
'name': "Mission Burrito",
'tags': "burrito, fajita"
}, instance=Place())
self.assertTrue(form.is_valid())
mission_burrito = form.save()
reloaded_mission_burrito = Place.objects.get(pk=mission_burrito.pk)
self.assertEqual(
set(reloaded_mission_burrito.tags.all()),
set([Tag.objects.get(name='burrito'), Tag.objects.get(name='fajita')])
)
示例4: test_create_with_tags_with_plain_taggable_manager
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def test_create_with_tags_with_plain_taggable_manager(self):
class PlaceForm(ClusterForm):
class Meta:
model = NonClusterPlace
exclude_formsets = ['tagged_items', 'reviews']
fields = ['name', 'tags']
form = PlaceForm({
'name': "Mission Burrito",
'tags': "burrito, fajita"
}, instance=NonClusterPlace())
self.assertTrue(form.is_valid())
mission_burrito = form.save()
reloaded_mission_burrito = NonClusterPlace.objects.get(pk=mission_burrito.pk)
self.assertEqual(
set(reloaded_mission_burrito.tags.all()),
set([Tag.objects.get(name='burrito'), Tag.objects.get(name='fajita')])
)
示例5: get_context
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
if self.tag_model == Tag:
autocomplete_url = reverse('wagtailadmin_tag_autocomplete')
else:
autocomplete_url = reverse(
'wagtailadmin_tag_model_autocomplete',
args=(self.tag_model._meta.app_label, self.tag_model._meta.model_name)
)
if self.free_tagging is None:
free_tagging = getattr(self.tag_model, 'free_tagging', True)
else:
free_tagging = self.free_tagging
context['widget']['autocomplete_url'] = autocomplete_url
context['widget']['options_json'] = json.dumps({
'allowSpaces': getattr(settings, 'TAG_SPACES_ALLOWED', True),
'tagLimit': getattr(settings, 'TAG_LIMIT', None),
'autocompleteOnly': not free_tagging,
})
return context
示例6: test_tags_autocomplete_custom_model
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def test_tags_autocomplete_custom_model(self):
response = self.client.get(
reverse('wagtailadmin_tag_model_autocomplete', args=('tests', 'restauranttag')),
{'term': 'ital'}
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'application/json')
data = json.loads(response.content.decode('utf-8'))
self.assertEqual(data, ['Italian'])
# should not return results from the standard Tag model
response = self.client.get(
reverse('wagtailadmin_tag_model_autocomplete', args=('tests', 'restauranttag')),
{'term': 'test'}
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'application/json')
data = json.loads(response.content.decode('utf-8'))
self.assertEqual(data, [])
示例7: post_list
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def post_list(request, tag_slug=None):
object_list = Post.published.all()
tag = None
if tag_slug:
tag = get_object_or_404(Tag, slug=tag_slug)
object_list = object_list.filter(tags__in=[tag])
paginator = Paginator(object_list, 3) # 3 posts in each page
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer deliver the first page
posts = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
posts = paginator.page(paginator.num_pages)
return render(request,
'blog/post/list.html',
{'page': page,
'posts': posts,
'tag': tag})
示例8: add_arguments
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def add_arguments(self, parser):
super().add_arguments(parser)
count = self.subs.add_parser('count', help='Count tag usage')
count.add_argument('name', help='Tag name we want to count.')
count.set_defaults(func=self.count)
delete = self.subs.add_parser('delete', help='Delete tag')
delete.add_argument('name', help='Tag name we want to delete.')
delete.add_argument('--force', action='store_true',
help='Force delete even if tag is still used.')
delete.set_defaults(func=self.delete)
rename = self.subs.add_parser('rename', help='Rename a tag')
rename.add_argument('old', help='Old name.')
rename.add_argument('new', help='New name.')
rename.set_defaults(func=self.rename)
replace = self.subs.add_parser('replace',
help='Replace tag by another and delete it')
replace.add_argument('old', help='Old tag name.')
replace.add_argument('new', help='New tag name.')
replace.set_defaults(func=self.replace)
sanitize = self.subs.add_parser('sanitize',
help=('Sanitize existing tags.\n'
'Remove duplicates, clean characters...'))
sanitize.set_defaults(func=self.sanitize)
list_ = self.subs.add_parser('list', help='List tags')
list_.set_defaults(func=self.list)
示例9: get_tag_or_exit
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def get_tag_or_exit(self, name):
tag = Tag.objects.filter(name=name).first()
if not tag:
exit('No tag found with name "{}"'.format(name))
return tag
示例10: delete
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def delete(self, options):
name = options['name']
tag = self.get_tag_or_exit(name)
count = self._count(name)
force = options.get('force')
if count and not force:
confirm = input('Tag "{}" is still linked to {} items.\n'
'Type "yes" to confirm delete or "no" to '
'cancel: '.format(name, count))
if confirm != 'yes':
exit("Delete cancelled.")
tag.delete()
notice('Deleted tag "{name}".'.format(**options))
示例11: rename
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def rename(self, options):
if options['old'] == options['new']:
exit('Nothing to rename, tags are equal.')
tag = self.get_tag_or_exit(options['old'])
if Tag.objects.filter(name=options['new']).exclude(pk=tag.pk).exists():
exit('Tag "{new}" already exists. Aborting.'.format(**options))
tag.name = options['new']
tag.save()
notice('Renamed "{old}" to "{new}".'.format(**options))
示例12: replace
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def replace(self, options):
if options['old'] == options['new']:
exit('Nothing to rename, tags are equal.')
old = self.get_tag_or_exit(options['old'])
new, created = Tag.objects.get_or_create(name=options['new'])
if created:
notice('Created tag "{new}"'.format(**options))
relations = TaggedItem.objects.filter(tag=old)
for relation in relations:
content = relation.content_object
notice('Processing "{}"'.format(repr(content)))
relation.delete()
content.tags.add(new)
old.delete()
notice('Deleted "{}"'.format(old))
示例13: test_rename_should_rename_tag
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def test_rename_should_rename_tag():
doc = DocumentFactory(tags=['tag1'])
tag1 = Tag.objects.get(name='tag1')
call_command('tags', 'rename', 'tag1', 'tag2')
assert not Tag.objects.filter(name='tag1')
assert 'tag2' in doc.tags.names()
assert 'tag1' not in doc.tags.names()
assert tag1.id == Tag.objects.get(name='tag2').id
示例14: test_rename_should_exit_on_non_existing_tag
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def test_rename_should_exit_on_non_existing_tag():
DocumentFactory(tags=['tag1'])
with pytest.raises(SystemExit):
call_command('tags', 'rename', 'tag3', 'tag2')
assert Tag.objects.filter(name='tag1')
示例15: test_rename_should_exit_if_new_name_already_exists
# 需要导入模块: from taggit import models [as 别名]
# 或者: from taggit.models import Tag [as 别名]
def test_rename_should_exit_if_new_name_already_exists():
DocumentFactory(tags=['tag1'])
Tag.objects.create(name='tag2')
with pytest.raises(SystemExit):
call_command('tags', 'rename', 'tag1', 'tag2')
assert Tag.objects.filter(name='tag1')
assert Tag.objects.filter(name='tag2')