Search code examples
pythondjangoelasticsearchdjango-haystack

ImportError : cannot import name generic


I have installed Django 1.10. I need to import the loafing but not able to import it. I am trying to connect my django app with elasticsearch using Haystack and wanted to build index. The code I want to run is

python manage.py rebuild_index

The error I get is:

base ---dir
/Manish/Projects/Spark/ad-tracking-django-env/ad-tracking-django
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Manish/Projects/Spark/ad-tracking-django-env/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/Manish/Projects/Spark/ad-tracking-django-env/lib/python2.7/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/Manish/Projects/Spark/ad-tracking-django-env/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Manish/Projects/Spark/ad-tracking-django-env/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Manish/Projects/Spark/ad-tracking-django-env/lib/python2.7/site-packages/django/apps/config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Manish/Projects/Spark/ad-tracking-django-env/lib/python2.7/site-packages/catalog/models.py", line 4, in <module>
    from django.contrib.contenttypes import generic
ImportError: cannot import name generic

The main error is:

ImportError: cannot import name generic


Solution

  • loading class has been removed in django 1.9 so that's why you are getting the error. Instead you can use apps class.

    >>> from django.apps import apps
    

    EDIT: generic class is also removed in v1.9 and functionality has been moved to models class and fields class.

    >>> from django.contrib.contenttypes.models import ContentType
    >>> from django.contrib.contenttypes.fields import GenericForeignKey
    

    More info can be found here