1

I am upgrading to django 1.9 (from 1.6)

mysql-connector-python 2.1.6 is installed

when I try to run my app, I get:

TypeError: Error when calling the metaclass bases
    'NoneType' object is not callable

Has anyone had this issue?

stacktrace:

/Library/Python/2.7/site-packages/django/core/handlers/wsgi.pyc in __init__(self, *args, **kwargs)
    149     def __init__(self, *args, **kwargs):
    150         super(WSGIHandler, self).__init__(*args, **kwargs)
--> 151         self.load_middleware()
    152 
    153     def __call__(self, environ, start_response):

/Library/Python/2.7/site-packages/django/core/handlers/base.pyc in load_middleware(self)
     55             for middleware_path in settings.MIDDLEWARE_CLASSES:
     56                 print middleware_path
---> 57                 mw_class = import_string(middleware_path)
     58                 try:
     59                     mw_instance = mw_class()

/Library/Python/2.7/site-packages/django/utils/module_loading.pyc in import_string(dotted_path)
     18         six.reraise(ImportError, ImportError(msg), sys.exc_info()[2])
     19 
---> 20     module = import_module(module_path)
     21 
     22     try:

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.pyc in import_module(name, package)
     35             level += 1
     36         name = _resolve_name(name[level:], package, level)
---> 37     __import__(name)
     38     return sys.modules[name]

/Library/Python/2.7/site-packages/django/contrib/auth/middleware.py in <module>()
      2 from django.contrib import auth
      3 from django.contrib.auth import load_backend
----> 4 from django.contrib.auth.backends import RemoteUserBackend
      5 from django.core.exceptions import ImproperlyConfigured
      6 from django.utils.deprecation import MiddlewareMixin

/Library/Python/2.7/site-packages/django/contrib/auth/backends.py in <module>()
      2 
      3 from django.contrib.auth import get_user_model
----> 4 from django.contrib.auth.models import Permission
      5 
      6 UserModel = get_user_model()

/Library/Python/2.7/site-packages/django/contrib/auth/models.py in <module>()
      2 
      3 from django.contrib import auth
----> 4 from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
      5 from django.contrib.auth.signals import user_logged_in
      6 from django.contrib.contenttypes.models import ContentType

/Library/Python/2.7/site-packages/django/contrib/auth/base_user.py in <module>()
     50 
     51 @python_2_unicode_compatible
---> 52 class AbstractBaseUser(models.Model):
     53     password = models.CharField(_('password'), max_length=128)
     54     last_login = models.DateTimeField(_('last login'), blank=True, null=True)

/Library/Python/2.7/site-packages/django/db/models/base.pyc in __new__(cls, name, bases, attrs)
    122                 app_label = app_config.label
    123 
--> 124         new_class.add_to_class('_meta', Options(meta, app_label))
    125         if not abstract:
    126             new_class.add_to_class(

/Library/Python/2.7/site-packages/django/db/models/base.pyc in add_to_class(cls, name, value)
    328         # We should call the contribute_to_class method only if it's bound
    329         if not inspect.isclass(value) and hasattr(value, 'contribute_to_class'):
--> 330             value.contribute_to_class(cls, name)
    331         else:
    332             setattr(cls, name, value)

/Library/Python/2.7/site-packages/django/db/models/options.pyc in contribute_to_class(self, cls, name)
    212         if not self.db_table:
    213             self.db_table = "%s_%s" % (self.app_label, self.model_name)
--> 214             self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
    215 
    216     def _prepare(self, model):

/Library/Python/2.7/site-packages/django/db/__init__.pyc in __getattr__(self, item)
     31     """
     32     def __getattr__(self, item):
---> 33         return getattr(connections[DEFAULT_DB_ALIAS], item)
     34 
     35     def __setattr__(self, name, value):

/Library/Python/2.7/site-packages/django/db/utils.pyc in __getitem__(self, alias)
    210         db = self.databases[alias]
    211         backend = load_backend(db['ENGINE'])
--> 212         conn = backend.DatabaseWrapper(db, alias)
    213         setattr(self._connections, alias, conn)
    214         return conn

/Library/Python/2.7/site-packages/mysql/connector/django/base.pyc in __init__(self, *args, **kwargs)
    334 
    335     def __init__(self, *args, **kwargs):
--> 336         super(DatabaseWrapper, self).__init__(*args, **kwargs)
    337 
    338         try:

/Library/Python/2.7/site-packages/django/db/backends/base/base.pyc in __init__(self, settings_dict, alias, allow_thread_sharing)
     94         self.run_commit_hooks_on_set_autocommit_on = False
     95 
---> 96         self.client = self.client_class(self)
     97         self.creation = self.creation_class(self)
     98         self.features = self.features_class(self)

TypeError: Error when calling the metaclass bases
    'NoneType' object is not callable

Update: i added

client_class = DatabaseClient 
creation_class = DatabaseCreation
features_class = DatabaseFeatures
introspection_class = DatabaseIntrospection
ops_class = DatabaseOperations
validation_class = DatabaseValidation 

in mysql/connecotr/django/base.py at DatabaseWrapper (looks like missing? - its there on django1.11/db/backends/mysql/base.py DatabaseWrapper). but now i get the error:

 Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

so i dont know if this solved the issue or created a new one

Eyal Ch
  • 9,552
  • 5
  • 44
  • 54
  • As an aside, upgrading from 1.6 to 1.9 is a big jump. You might have fewer problems if you go to 1.8 first (ideally via 1.7). Once you've done that, upgrading from Django 1.8 LTS to Django 1.11 LTS should be more straight forward. – Alasdair Apr 26 '17 at 13:39
  • Do you get the same error if you install the latest version of `mysql-connector-python`, or use the built in `django.db.backends.mysql` backend with `mysqlclient`? – Alasdair Apr 26 '17 at 13:44
  • i tried to install the latest version of mysql-connector-python. same error. – Eyal Ch Apr 26 '17 at 13:46
  • 1
    I just did a fresh install of django 1.11 and then I installed the most recent version of mysql-connector-python. pip3 install --egg http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.1.6.zip I have the same error after I run python3 /home/django/workspace/mysite/manage.py migrate – pitchblack408 Apr 27 '17 at 06:18
  • @pitchblack408 so you have the same error? have you managed to fix it? – Eyal Ch Apr 27 '17 at 06:27
  • Nope, not yet. I am going to define a model like the one in the tutorial and then see if I still get the same error. – pitchblack408 Apr 27 '17 at 06:45
  • @EyalCh , I disabled the settings for mysql-connector-python in the settings file. Then enabled the db.sqlite3 setting and re ran manage.py migrate. It ran fine, it does seem that this is an issue with mysql-connector-python – pitchblack408 Apr 27 '17 at 06:58
  • @pitchblack408 maybe i managed to overcome this: i added client_class = DatabaseClient, creation_class = DatabaseCreation,features_class = DatabaseFeatures, introspection_class = DatabaseIntrospection, ops_class = DatabaseOperations, validation_class = DatabaseValidation in mysql/connecotr/django/base.py at DatabaseWrapper (looks like missing? - its there on django1.11/db/backends/mysql/base.py DatabaseWrapper). but now i get the error Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS. – Eyal Ch Apr 27 '17 at 07:01
  • @EyalCh please do not write code in comments. Edit your question to reflect what progress you have made. – Shai Apr 27 '17 at 11:04
  • Do you really need `mysql-connector-python`? Why not use the built in `django.db.backends.mysql` backend with `mysqlclient`? – Alasdair Apr 27 '17 at 12:01

1 Answers1

0

Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

This error generally happens when your apps get imported before the settings files are completely imported (i.e. before the initialization of INSTALLED_APPS, which happen after settings have finished importing). So make sure you don't have any code in settings file, which imports code from some other apps.

Also ensure that you are not importing models or similar app code, in __init__.py files of your apps, such that there imports are not automatically triggered at settings level.

hspandher
  • 15,934
  • 2
  • 32
  • 45
  • the error that you point here is "TypeError: Error when calling the metaclass bases" or the "doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS" – Eyal Ch Apr 27 '17 at 14:06
  • The latter one. – hspandher Apr 27 '17 at 14:08
  • stuck with the same error on Django up-gradation from 1.8 to 1.11. @EyalCh , how did you fix it? – Ankush Dec 08 '17 at 10:54
  • @Ankush didnt fix it yet, we stop django 1.11 upgrading. please let me know if you managed to fix it. – Eyal Ch Dec 08 '17 at 20:04