I redefined the User model:

class Profile(AbstractUser): class Meta: ordering = ['id'] verbose_name = u'человека' verbose_name_plural = u'Люди' 

I registered it in settings.py :

 AUTH_USER_MODEL = 'main.Profile' 

where main is the name of my application.

In admin.py :

 from .models import Profile admin.site.register(Profile) 

And in general, everything worked fine, tested. Filled with test users. The problem appeared after I added the models for the dialogue in the new dialogue.py file to the subfolders of the models my project (along with __init__.py )

 from django.db import models from main.models import Profile class Dialogue(models.Model): Partakers = models.ManyToManyField(Profile) class Message(models.Model): Sender = models.ForeignKey(Profile) 

Mistake:

  (hello) D:\django\site\hello>d: & cd D:\django\hello\Scripts & activate.bat & cd D:\django\site\hello & python manage.py runserver Unhandled exception in thread started by <function wrapper at 0x02B93B70> Traceback (most recent call last): File "D:\django\hello\lib\site-packages\django\utils\autoreload.py", line 228, in wrapper fn(*args, **kwargs) File "D:\django\hello\lib\site-packages\django\core\management\commands\runser ver.py", line 116, in inner_run autoreload.raise_last_exception() File "D:\django\hello\lib\site-packages\django\utils\autoreload.py", line 251, in raise_last_exception six.reraise(*_exception) File "D:\django\hello\lib\site-packages\django\utils\autoreload.py", line 228, in wrapper fn(*args, **kwargs) File "D:\django\hello\lib\site-packages\django\__init__.py", line 27, in setup apps.populate(settings.INSTALLED_APPS) File "D:\django\hello\lib\site-packages\django\apps\registry.py", line 116, in populate app_config.ready() File "D:\django\hello\lib\site-packages\django\contrib\admin\apps.py", line 23 , in ready self.module.autodiscover() File "D:\django\hello\lib\site-packages\django\contrib\admin\__init__.py", lin e 26, in autodiscover autodiscover_modules('admin', register_to=site) File "D:\django\hello\lib\site-packages\django\utils\module_loading.py", line 50, in autodiscover_modules import_module('%s.%s' % (app_config.name, module_to_search)) File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module __import__(name) File "D:\django\hello\lib\site-packages\django\contrib\auth\admin.py", line 7, in <module> from django.contrib.auth.forms import ( File "D:\django\hello\lib\site-packages\django\contrib\auth\forms.py", line 22 , in <module> UserModel = get_user_model() File "D:\django\hello\lib\site-packages\django\contrib\auth\__init__.py", line 199, in get_user_model "AUTH_USER_MODEL refers to model '%s' that has not been installed" % setting s.AUTH_USER_MODEL django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'ma in.Profile' that has not been installed 

Models deleted. Then he deleted the file. Restarted the runserver, but the error remains

I found a similar problem on the web, but I still don’t understand how to fix it.

  • Replace image with text. - Sergey Gornostaev
  • @SergeyGornostaev, I know that this is not accepted, but I do not use ide, I launch everything from the console, so I can’t just copy the text of the error. And to rewrite manually from the image for quite a long time and I can make typos, could we do without it? - digital-mag
  • Why can not you? From the console, the text is copied. - Sergey Gornostaev
  • @SergeyGornostaev, for sure. Copy happened. Replaced with text the picture - digital-mag
  • @SergeyGornostaev, in general, by trial and error, it got to the get_model method in django.apps.config.AppConfig. When I try to get the current model: self.models [model_name.lower ()], I get this error. For some reason self.models does not contain any models. How can this be? - digital-mag

1 answer 1

If you create the main.models module as a directory, then the main/models/__init__.py file is responsible for its contents.

If the main/models/__init__.py empty, then Django will not see any models and will main/models/__init__.py an error.

All submodules inside the main.models module will not be automatically imported by anyone; you need to do it yourself. For example, if you have a Profile model in main/models/profile.py , then you need to write something like this in main/models/__init__.py as follows:

 import main.models.profile 

or

 from . import profile 

or register the model explicitly:

 from .profile import Profile 

All methods are equivalent in the sense that the profile submodule will load in all cases and Django will be able to detect the main.Profile model, which is written in AUTH_USER_MODEL .


At some point, you tried to put the Profile model in main/models/__init__.py and get it in main/models/profile.py like this:

 from main.models import Profile 

In general, this does not work, because most likely it will result in cyclic imports. As I said above, you need to load the profile.py file with import of the import main.models.profile , but it turns out that this file depends on from main.models import Profile , but the __init__.py file depends on import main.models.profile , but the profile.py file depends on from main.models import Profile , but the __init__.py file depends on import main.models.profile and so on ad infinitum. When the code in the profile.py file is executed, the code for creating the Profile model in the __init__.py file has not been trivially completed - as a result, everything breaks down to hell, and the Profile simply does not show up.

This can be solved by moving the import main.models.profile to the very end of the __init__.py file - then the Profile model will have time to be created before the profile.py file is loaded. But it is better, in the name of cleanliness of the architecture, to get rid of cyclic imports and not to place any models at all in the __init__.py file and leave only the submodule imports there.