开发者

django tagging - filter by tag

I'm on a free-time project, and I have this model:

class Post(models.Model):
    title = models.CharField(max_length=255)
    <..>
    tags = TagAutocompleteField()

TagAutocompleteField() is same as TagField() from django-tagging witch is simple CharField so print post.tags will give 'one two three' instead of ['one', 'two', 'three',].

Also i have a view:

def tagged(request, tag_id):
    tag = get_object_or_404(Tag, pk=tag_id)
    post_list = Post.objects.all() \
                .filter(tags__sp开发者_StackOverflow社区lit__in=tag) \                                                          
                .filter(is_published=True) \
                .order_by('-time_publish')
    return render_to_response('plugins/persona/list.html', {
                              'post_list': post_list,
                              }) 

The problem is that i cannot filter all the posts that have specific tag since tags is charField i've tried to use split() but filters does not allow it.

People suggest to use this function to get tag list:

def get_tags(self):                                                                                 
    return Tag.objects.get_for_object(self)

but yet i cannot it use in filters also.

How should i get all the posts that have same tag? Usual way with tagging is to get objects by tag but if tags are used by few applications it may give more than posts.


Try using regex

.filter(tags__iregex=r'\b%s\b' % tag)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜