Kabir
Kabir

Reputation: 1479

Getting error with django-taggit with admin

In a Django project I installed django_taggit. I'm getting this error when I syncdb my project.

AttributeError: 'TaggableManager' object has no attribute 'related'

My models.py something like this...

from taggit.managers import TaggableManager

class Post(models.Model):
    title = models.CharField(max_length=100)
    tags = TaggableManager()

and admin.py something like this...

from django.contrib import admin

admin.site.register(Post)

Upvotes: 1

Views: 944

Answers (1)

Hamms
Hamms

Reputation: 5107

Django admin is trying to use the TaggableManager to manage your post objects. You need to be careful when using custom managers; as the docs specify:

If you use custom Manager objects, take note that the first Manager Django encounters (in the order in which they’re defined in the model) has a special status. Django interprets the first Manager defined in a class as the “default” Manager, and several parts of Django (including dumpdata) will use that Manager exclusively for that model. As a result, it’s a good idea to be careful in your choice of default manager in order to avoid a situation where overriding get_query_set() results in an inability to retrieve objects you’d like to work with.

An easy way to get around this is to manually specify Post.objects first:

class Post(models.Model):
    title = models.CharField(max_length=100)
    objects = models.Manager()
    tags = TaggableManager()

Upvotes: 1

Related Questions