[Django]-Indexing Taggit tags with Algolia for Django: '_TaggableManager' object has no attribute 'name'

2👍

To index the taggit tags with your Post fields, you will need to expose a callable that returns a Blog Post’s tags as a list of strings.

The best option is to store them as _tags, which will let you filter on tags at query time.

Your PostIndex would look like this:

class PostIndex(AlgoliaIndex):
    fields = ('title', '_tags')
    settings = {'searchableAttributes': ['title']}
    index_name = 'Blog Posts Index'
    should_index = 'is_published'

As for Post:

class Post(models.Model):
    # ...some model fields...

    tags = TaggableManager()

    def _tags(self):
        return [t.name for t in self.tags.all()]

Following these instructions, your records will be indexed with their respective tags:

Screenshot of a tagAlgolia explorer

You can check the taggit branch of our Django demo, which demonstrates these steps.

👤PLNech

1👍

To answer my own question. I have now passed in both the model and the model index so Algolia now knows what to index and what not to index. Although I would like a method to allow Algolia to index taggit tags, alas, it is probably not possible.

My apps.py file:

import algoliasearch_django as algoliasearch
from django.apps import AppConfig
from .index import PostIndex

class BlogConfig(AppConfig):
    name = 'blog'

    def ready(self):
        Post = self.get_model('Post')
        algoliasearch.register(Post, PostIndex)

My index.py file:

from algoliasearch_django import AlgoliaIndex

class PostIndex(AlgoliaIndex):
    fields = ('title')
    settings = {'searchableAttributes': ['title']}
    index_name = 'Blog Posts Index'
    should_index = 'is_published'

And that should pretty much work! Simple when you know how, or after trying about 10 different options!

0👍

So since nobody is answering I tell you how I solved this issue but I have to say that it is not a nice Way and not a “clean” Solution at all. So what I did is went into “taggit managers” in the site-packages (env->lib->python2.x/3.x-> site_packages->taggit->managers.py) In the managers.py file you will find at line 394 this beautiful piece of code:

def __get__(self, instance, model):
            if instance is not None and instance.pk is None:
                raise ValueError("%s objects need to have a primary key value "
                                 "before you can access their tags." % model.__name__)
            manager = self.manager(
                through=self.through,
                model=model,
                instance=instance,
                prefetch_cache_name=self.name,  # this is the line I comment out when building the index,
                name=self.name  #this is the line I added and needs to be commented out after the index is build. 
            )
            return manager

So what I do when I want to rebuild the search index is comment out (putting”#” infront of the line) prefetch_cache_name=self.name, and replace it with name=self.name. So building the index will work. After the Index is finished building, you have to bring everything back as it was before (switch the “#” to name=self.name again and leave prefetch_cache_name=self.name, visible again).

As already mentioned this is probably not the best way but I had the same pain and this is working for me. It takes one minute when you have the routine. Since I have to rebuild the Index maybe once every two weeks, that isn’t such a deal for me but if you have to do it very often this might be annoying…

Anyway I hope that helps you.

0👍

It can help you if you using django==2+

The problem is in get_queryset() method of TaggableManager

Open file with it (my path was: Pipenv(project_name)/lib/site-packages/taggit/manager.py)
Find _TaggableManager class and change method name get_queryset to get_query_set

Done. I wish taggit’s developers will fixed this in future updates

Leave a comment