Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic way to use multiple indexes per models #252

Open
clemfromspace opened this issue May 5, 2018 · 3 comments
Open

Dynamic way to use multiple indexes per models #252

clemfromspace opened this issue May 5, 2018 · 3 comments
Labels
Enhancement request for an improvement.

Comments

@clemfromspace
Copy link
Contributor

clemfromspace commented May 5, 2018

Hello there :)

Recently, I found myself in the needs to add multiple indexes per models, but in a dynamic way.

I used django-hvad for the model translations, and needed to "route" the models to a different index based on the language_code field of the model.

Do you know how if there is a way I could achieve this ?

@julienbourdeau
Copy link
Contributor

I don't think it's possible today but it's definitely something that we should handle.

I'm thinking we could have a new method get_index_name and move this logic into it: https://github.com/algolia/algoliasearch-django/blob/master/algoliasearch_django/models.py#L164-L171 then everyone would be able to override it. What do you think?

@PLNech
Copy link
Member

PLNech commented May 14, 2018

I would use the same convention as for tags: if it's a member just take it, if it's a callable call it everytime we need its value. This way we don't introduce a new get_xxx method, we just replace the xxx field by a callable :)

@clemfromspace
Copy link
Contributor Author

clemfromspace commented May 14, 2018

The tags solution looks good to me, if I understand correctly and if it gets implemented I should be able to do the following for a django model:

class MyTranslatableModel(models.Model):
    language_code = models.CharField(max_length=2)

    @property  # Or any callable / attribute
    def index_name(self):
        return 'index_name_{}'.format(self.language_code)

If that's the case, it will be perfect!

I will gladly help implementing this :)

@clemfromspace clemfromspace added the Enhancement request for an improvement. label May 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement request for an improvement.
Projects
None yet
Development

No branches or pull requests

3 participants