Re: Cannot resolve keyword 'content_object' into field.

386 views
Skip to first unread message

westdot...@gmail.com

unread,
Oct 31, 2013, 2:35:23 PM10/31/13
to django...@googlegroups.com
(Replying to a year-old post here, but hopefully it'll help the next person like me who gets the same error and googles for it...)

As per the documentation at https://github.com/alex/django-taggit/blob/develop/docs/custom_tagging.txt , taggit requires the foreign key from the 'tagged item' class back to the model to be named 'content_object'. In this case, TaggedAlbum's 'aid' field needs to be renamed 'content_object'.

- Matt

On Wednesday, September 19, 2012 4:12:29 PM UTC+1, Andy wrote:
I am attempting to follow this tutorial to have a hierarchical tag structure. http://tentacles.posterous.com/django-hierarchical-tags-with-taggit-and-tree

My code looks like this:

----models.py---
from taggit.models import TagBase, ItemBase
from taggit.managers import TaggableManager
from treebeard.mp_tree import MP_Node

class HierarchicalTag(TagBase, MP_Node):
    node_order_by = [ 'name' ]

class TaggedAlbum(ItemBase):
    aid = models.ForeignKey('Album')
    tag = models.ForeignKey('HierarchicalTag', related_name='tags')

class Album(ItemBase):
    name = models.CharField('Album Name',max_length=45, null=False, blank=False)
    description = models.CharField('Album Description',max_length=255, null=False, blank=False)
    tags = TaggableManager(through=TaggedAlbum, blank=True)

Now if I run this code, I get a stack trace:
>>> from photos.models import *
>>> a1 = Album(name='a1',description='desc')
>>> a1.save()
>>> a1
<Album: a1>
>>> a1.tags
<taggit.managers._TaggableManager object at 0x02E15390>
>>> a1.tags.all()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 116, in all
    return self.get_query_set()
  File "C:\DEV\PM\taggit\managers.py", line 150, in get_query_set
    return self.through.tags_for(self.model, self.instance)
  File "C:\DEV\PM\photos\models.py", line 24, in tags_for
    '%s__content_object' % cls.tag_relname(): instance
  File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 143, in  filter
    return self.get_query_set().filter(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 621, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 639, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1250, in add_q
    can_reuse=used_aliases, force_having=force_having)
  File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1122, in add_filter
    process_extras=process_extras)
  File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1316, in setup_joins
    "Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'content_object' into field. Choices are: aid, id, tag
>>>


What does this 'content_object' mean? How do I fix it?
Reply all
Reply to author
Forward
0 new messages