django-taggit深度关系查询

时间:2011-07-03 00:10:40

标签: django django-orm django-taggit

我正在使用django-taggit,在尝试过滤关系时遇到了问题。

拥有以下型号:

class Artist(models.Model):
     tags = TaggableManager()


class Gig(models.Model):
    artist = models.ManyToManyField(Artist)

我想要达到的是让所有艺术家都有特定标签的演出。

我认为这很容易并急切地写道:

Gig.objects.filter(artist__tags__name__in=["rock"])

哪位给了我:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/manager.py", line  141, in filter
return self.get_query_set().filter(*args, **kwargs)
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/query.py", line 550, in filter
  return self._filter_or_exclude(False, *args, **kwargs)
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/query.py", line 568, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1172, in add_q
can_reuse=used_aliases, force_having=force_having)
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1139, in add_filter
process_extras=False)
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1060, in add_filter
negate=negate, process_extras=process_extras)
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1238, in setup_joins
"Choices are: %s" % (name, ", ".join(names)))
 FieldError: Cannot resolve keyword 'tagged_items' into field. Choices are: artist, date, id, location, url

2 个答案:

答案 0 :(得分:1)

我设法通过在 manage.py 中评论 TaggableManager.extra_filters()来解决此问题。

带上一粒盐,因为我不知道这可能会因为这个而破坏。

答案 1 :(得分:1)

为所有艺术家制作特定标签的所有演出。

artists = Artist.objects.filter(tags__name__in=["rock"])
gigs = Gig.objects.filter(artist__in=artists)
相关问题