1
我在一个free-time project过滤器,我有这个模型:Django的标记 - 由标签
class Post(models.Model):
title = models.CharField(max_length=255)
<..>
tags = TagAutocompleteField()
TagAutocompleteField()
是相同TagField()
从django-tagging女巫简单CharField
所以print post.tags
会给'one two three'
而不是['one', 'two', 'three',]
。
此外,我有一个观点:
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)
但我还是不能把它在过滤器中也可以使用。
我应该如何获得所有具有相同标签的帖子?标记的常用方式是通过标记获取对象,但如果标记仅由少数应用程序使用,则可能不仅仅是发布帖子。
可能几乎所有的世界问题可以用正则表达式解决...它的工作原理谢谢:) – JackLeo 2011-06-16 07:26:27
应该有注意,MySQL正则表达式在Python不同。 – JackLeo 2011-08-29 09:34:24