Django-tagging:通过指定多个标签来抓取对象?

时间:2010-06-19 21:57:17

标签: django django-tagging

我目前在urls.py中有一个条目,它为我的错误提取了单独的永久链接:

from django.conf.urls.defaults import *

from tagging.views import tagged_object_list

from bugs.models import Bug

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^workarounds/', include('workarounds.foo.urls')),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
    # to INSTALLED_APPS to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    (r'^$', 'django.views.generic.simple.direct_to_template', {'template':'homepage.html'}),

    (r'^bugs/(?P<slug>[-\w]+)/$', 'bugs.views.bug_detail'),
    (r'^bugs/tagged/(?P<tag>[^/]+)/$', 
    'tagging.views.tagged_object_list',
    {
        'queryset_or_model': Bug,
        'template_name' : 'tag/lone.html'}),
    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),
)

因此,如果我指定了一个名为bugs/tagged/firefox的网址,则会显示firefox标记。我如何通过多个标签过滤掉它?例如:firefox+css将返回标有firefoxcss的所有对象。

1 个答案:

答案 0 :(得分:3)

您必须构建自己的视图,而不是使用tagging.views.tagged_object_list

(r'^bugs/tagged/(?P<tags>[-\w+]+)/$', your_tag_view)

在您的视图中,获取您要搜索的代码列表:

tags = tags.split('+')

然后,使用TaggedItem.objects.get_by_model查询,方便地接受标记列表:

from tagging.models import TaggedItem
bugs = TaggedItem.objects.get_by_model(Bug, tags)