我正在使用free-time project ,我有这个模型:
class Post(models.Model):
title = models.CharField(max_length=255)
<..>
tags = TagAutocompleteField()
TagAutocompleteField()
与 django-tagging 中的 TagField()
相同女巫很简单 CharField
所以 print post.tags
会给出 '一二三'
而不是 ['one', 'two' ,“三”,]
。
我也有一个观点:
def tagged(request, tag_id):
tag = get_object_or_404(Tag, pk=tag_id)
post_list = Post.objects.all() \
.filter(tags__split__in=tag) \
.filter(is_published=True) \
.order_by('-time_publish')
return render_to_response('plugins/persona/list.html', {
'post_list': post_list,
})
问题是我无法过滤所有具有特定标签的帖子,因为标签是 charField
我尝试使用 split()
但过滤器不允许这样做.
人们建议使用这个函数来获取标签列表:
def get_tags(self):
return Tag.objects.get_for_object(self)
但我还不能在过滤器中使用它。
我应该如何获取所有具有相同标签的帖子?标记的通常方法是通过标签获取对象,但如果标签被少数应用程序使用,它可能会提供比帖子更多的内容。
最佳答案
尝试使用regex
.filter(tags__iregex=r'\b%s\b' % tag)
关于python - django 标记 - 按标记过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6364373/