algolia / algoliasearch-django

Seamless integration of Algolia into your Django project.
https://www.algolia.com
MIT License
173 stars 65 forks source link

register decorator gives TypeError #262

Closed qcaron closed 6 years ago

qcaron commented 6 years ago

Hi guys,

I defined this index by following the instructions found in the documentation:

index.py

from algoliasearch_django import AlgoliaIndex, register

from .models import TestSearch

@register(TestSearch)
class TestSearchIndex(AlgoliaIndex):
    fields = (
        'text',
    )

Here are my models.py and settings.py for this test case:

models.py

from django.db import models

class TestSearch(models.Model):
    text = models.CharField(max_length=100)

    class Meta:
        verbose_name = 'Test'
        verbose_name_plural = 'Tests'

settings.py

...
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'content',
    'algoliasearch_django',
]
...
ALGOLIA = {
    'APPLICATION_ID': 'xxxxxxxx',
    'API_KEY': 'xxxxxxx',
    'INDEX_SUFFIX': 'dev',
}

I am getting this stacktrace when running ./manage.py algolia_applysettings:

(algolia_tests) quentins-mbp:algolia_tests quentincaron$ ./manage.py algolia_applysettings
Traceback (most recent call last):
  File "./manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/quentincaron/.virtualenvs/algolia_tests/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/Users/quentincaron/.virtualenvs/algolia_tests/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
    django.setup()
  File "/Users/quentincaron/.virtualenvs/algolia_tests/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/quentincaron/.virtualenvs/algolia_tests/lib/python3.6/site-packages/django/apps/registry.py", line 120, in populate
    app_config.ready()
  File "/Users/quentincaron/.virtualenvs/algolia_tests/lib/python3.6/site-packages/algoliasearch_django/apps.py", line 11, in ready
    self.module.autodiscover()
  File "/Users/quentincaron/.virtualenvs/algolia_tests/lib/python3.6/site-packages/algoliasearch_django/__init__.py", line 46, in autodiscover
    autodiscover_modules('index')
  File "/Users/quentincaron/.virtualenvs/algolia_tests/lib/python3.6/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/quentincaron/PycharmProjects/algolia_tests/content/index.py", line 7, in <module>
    class TestSearchIndex(AlgoliaIndex):
TypeError: 'NoneType' object is not callable

I am using:

Is this a bug? If not, I can update the documentation based on your answers.

Using algoliasearch.register(YourModel) works except the settings.ALGOLIA.INDEX_SUFFIX is not taken into account and I need to customize my index as well.

Let me know if you need more about my issue. Quentin

qcaron commented 6 years ago

Following the approach given on algoliasearch-django-example fixed it. I used the approach in the example project some time ago but I thought the documentation was up-to-date and decided to review the way I register indexes.

Another thing is that it is the name of the model is displayed when using the algolia_applysettings command for example. It would be great to have a full index name (with prefix and suffix) because I thought those were not applied... Until I checked the dashboard and displayed the list of indexes in the Algolia app - in the dashboard.

Cheers!

julienbourdeau commented 6 years ago

Hi @qcaron,

Thanks for your valuable feedback. We'll look into it and update documentation. Unfortunately, for now, our doc is still manage with a private repository so you cannot edit it. This README.md is generated from our doc on algolia.com/doc.

clemfromspace commented 6 years ago

Hi @qcaron and thank you for reporting this,

As stated by @julienbourdeau, the doc is indeed wrong: You should import the register decorator from the algoliasearch_django.decorators module and not from the main algoliasearch_django module.

So your code of the index.py should looks like this:

from algoliasearch_django import AlgoliaIndex
from algoliasearch_django.decorators import register 

from .models import TestSearch

@register(TestSearch)
class TestSearchIndex(AlgoliaIndex):
    fields = (
        'text',
    )

Really sorry for this, we will update the documentation right away :)

qcaron commented 6 years ago

Thanks for your answers and for reacting promptly 🤘

clemfromspace commented 6 years ago

Closed via f753b04