简体   繁体   中英

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)

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM