Django-Taggit 获取随机标签,跳过那些未分配给 Post 的标签
Django-Taggit get random tags skipping those not assigned to a Post
我想在用户点击搜索栏时向他们提供关于随机标签的建议。到目前为止,我的代码 returns 是我想要的,但它也返回未分配给任何 Post IE 的标签:当我删除 post 或删除其标签时,这些标签仍在显示在建议中。
# Get the suggestions (In View)
suggestions = Tag.objects.all().distinct().order_by('?')[:5]
# Model
class Post(models.Model):
title = models.CharField(max_length=256)
disclaimer = models.CharField(max_length=256, blank=True)
BLOGS = 'blogs'
APPLICATIONS = 'applications'
GAMES = 'games'
WEBSITES = 'websites'
GALLERY = 'gallery'
PRIMARY_CHOICES = (
(BLOGS, 'Blogs'),
(APPLICATIONS, 'Applications'),
(GAMES, 'Games'),
(WEBSITES, 'Websites'),
)
content_type = models.CharField(max_length=256, choices=PRIMARY_CHOICES, default=BLOGS)
screenshot = models.CharField(max_length=256, blank=True)
tags = TaggableManager()
body = RichTextField()
date_posted = models.DateTimeField(default=datetime.now)
date_edited = models.DateTimeField(blank=True, null=True)
visible = models.BooleanField(default=True)
nsfw = models.BooleanField()
allow_comments = models.BooleanField(default=True)
files = models.ManyToManyField(File, blank=True)
def __str__(self):
if (self.visible == False):
return '(Hidden) ' + self.title + ' in ' + self.content_type
return self.title + ' in ' + self.content_type
如果您只想要分配给帖子的标签,您必须查询帖子的标签并选择其中五个:
allposts = Post.objects.all()
five_tags = list(set([tag.slug for post in allposts for tag in post.tags.all()]))[:5]
(使用set()
删除重复项)
编辑
如果你想在选择五个之前打乱所有标签,你可以这样做:
import random
allposts = Post.objects.all()
all_tags_list = list(set([tag.slug for post in allposts for tag in post.tags.all()]))
random.shuffle(all_tags_list)
five_tags = all_tags_list[:5]
我想在用户点击搜索栏时向他们提供关于随机标签的建议。到目前为止,我的代码 returns 是我想要的,但它也返回未分配给任何 Post IE 的标签:当我删除 post 或删除其标签时,这些标签仍在显示在建议中。
# Get the suggestions (In View)
suggestions = Tag.objects.all().distinct().order_by('?')[:5]
# Model
class Post(models.Model):
title = models.CharField(max_length=256)
disclaimer = models.CharField(max_length=256, blank=True)
BLOGS = 'blogs'
APPLICATIONS = 'applications'
GAMES = 'games'
WEBSITES = 'websites'
GALLERY = 'gallery'
PRIMARY_CHOICES = (
(BLOGS, 'Blogs'),
(APPLICATIONS, 'Applications'),
(GAMES, 'Games'),
(WEBSITES, 'Websites'),
)
content_type = models.CharField(max_length=256, choices=PRIMARY_CHOICES, default=BLOGS)
screenshot = models.CharField(max_length=256, blank=True)
tags = TaggableManager()
body = RichTextField()
date_posted = models.DateTimeField(default=datetime.now)
date_edited = models.DateTimeField(blank=True, null=True)
visible = models.BooleanField(default=True)
nsfw = models.BooleanField()
allow_comments = models.BooleanField(default=True)
files = models.ManyToManyField(File, blank=True)
def __str__(self):
if (self.visible == False):
return '(Hidden) ' + self.title + ' in ' + self.content_type
return self.title + ' in ' + self.content_type
如果您只想要分配给帖子的标签,您必须查询帖子的标签并选择其中五个:
allposts = Post.objects.all()
five_tags = list(set([tag.slug for post in allposts for tag in post.tags.all()]))[:5]
(使用set()
删除重复项)
编辑
如果你想在选择五个之前打乱所有标签,你可以这样做:
import random
allposts = Post.objects.all()
all_tags_list = list(set([tag.slug for post in allposts for tag in post.tags.all()]))
random.shuffle(all_tags_list)
five_tags = all_tags_list[:5]