5

I'm using django-taggit and I came upon a problem when trying to filter across relationships.

Having the following models:

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


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

What I would like to achieve is the get all gigs who's artist(s) have a specific tag.

I thought this would be easy and eagerly wrote:

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

Which gave me:

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
Jonas Geiregat
  • 5,214
  • 4
  • 41
  • 60
  • I see that you posted this a few months ago. Did you get it solved? – avatar Sep 28 '11 at 15:17
  • Sorry for the late reply. I never got it solved. I just switched to django-tagging which made such kind of behavior possible. But I have to warn you about it's API structure compared to django-taggit. Still it has some good elaborate documentation. – Jonas Geiregat Nov 17 '11 at 22:30
  • I have the same issue, as did another. More details on this django-taggit issue thread: https://github.com/alex/django-taggit/issues/84#issuecomment-3554357 – Anentropic Jan 18 '12 at 22:29
  • I wish I could remember why I decided to use taggit instead of django-tagging now...! :) – Anentropic Jan 18 '12 at 22:30

2 Answers2

1

I managed to fix it by commenting out TaggableManager.extra_filters() in manage.py.

Take it with a grain of salt, because I have no idea what I may have broken by doing this.

twig
  • 4,034
  • 5
  • 37
  • 47
1

Getting all Gigs for who's Artist's have a specific tag.

artists = Artist.objects.filter(tags__name__in=["rock"])
gigs = Gig.objects.filter(artist__in=artists)
Akshar Raaj
  • 14,231
  • 7
  • 51
  • 45