2011-07-03 75 views
5

我正在使用django-taggit,並在嘗試過濾關係時遇到問題。django-taggit深層關係查詢

具有以下型號:

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


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

我想實現的是讓所有演出誰的藝術家(S)有一個特定的標籤。

我想這是很容易和熱切寫道:

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 
+0

我看到你幾個月前發佈了這個。你解決了嗎? – avatar

+0

對不起,延遲迴復。我從來沒有解決它。我只是轉向django-tagging,這使得這種行爲成爲可能。但是與django-taggit相比,我必須警告你它的API結構。它仍然有一些很好的詳細文檔。 –

+0

謝謝你的回覆! – avatar

回答

1

我設法在註釋掉TaggableManager.extra_filters()修復管理它.py

帶上一粒鹽吧,因爲我不知道我這樣做會損壞什麼。

1

讓所有Gigs for who's Artist's都擁有特定標籤。

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