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:
You can check the taggit
branch of our Django demo, which demonstrates these steps.
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!
- [Django]-Handling database connection errors / operational error exceptions in Python / Django?
- [Django]-Django – field of language choices
- [Django]-Setting a foreign key on a formset on formset.save()
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.
- [Django]-How do you deploy a flask or django application using jwilder/nginx-proxy?
- [Django]-How to solve the did not return httpresonse error with django?
- [Django]-Django Queryset Count
- [Django]-Do I need to start a worker?
- [Django]-How to append string in the end of Django redirect in view?
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
- [Django]-Unknown command in django core managment
- [Django]-How to solve "No Python interpreter configured for the module"? in Android Studio?
- [Django]-Inner function raise in Django rest API
- [Django]-Debugging django templates in pycharm
- [Django]-How to import Django Settings to python standalone script