Django:它是如何找到用户模型的?

时间:2021-04-14 19:18:21

I have a question to the AUTH_USER_MODEL in Django: https://docs.djangoproject.com/en/dev/ref/settings/#auth-user-model

我对Django中的AUTH_USER_MODEL提出了一个问题:https://docs.djangoproject.com/en/dev/ref/settings/#auth-user-model

The default value is auth.User. However, the actual model is in auth.models.User. How does Django find the correct class?

默认值为auth.User。但是,实际模型位于auth.models.User中。 Django如何找到正确的课程?

I am asking because when I usually use models in Django, I have to write from myapp.models import MyModel. So, why do I not need models in auth.User for AUTH_USER_MODEL?

我问,因为当我通常在Django中使用模型时,我必须从myapp.models导入MyModel。那么,为什么我不需要auth.User中的模型用于AUTH_USER_MODEL?

Can some explain me that or show the code that uses it?

有人可以解释一下或显示使用它的代码吗?

1 个解决方案

#1


3  

Well you define models in the models.py file of an app. So that means that the module in which you stored the model class is app.models. Therefore the import reads:

那么你在应用程序的models.py文件中定义模型。这意味着存储模型类的模块是app.models。因此导入如下:

from app.models import MyModel

Django has in essence nothing to do with this: this is how Python loads modules and classes from these module(s).

Django本质上与此无关:这就是Python如何从这些模块加载模块和类。

Django however loads - when you for example run the server - the apps that are located in the INSTALLED_APPS list of the settings file (usually settings.py), and thus constructs a "register" where it stores Django models, and it names them in a uniform way: app_name.ModelName. There is no reason to specify models here, since models are defined in models.py, and it thus would only introduce "noise".

然而,当您运行服务器时,Django会加载位于设置文件的INSTALLED_APPS列表中的应用程序(通常是settings.py),从而构建一个存储Django模型的“寄存器”,并将其命名为统一的方式:app_name.ModelName。这里没有理由指定模型,因为模型是在models.py中定义的,因此它只会引入“噪声”。

You can obtain a reference to the model class with apps.get_model [Django-doc]

您可以使用apps.get_model [Django-doc]获取对模型类的引用

from django.apps import apps

apps.get_model('app_name', 'ModelName')

It thus then checks the registers of the loaded models, and returns a reference to the model.

然后,它检查加载的模型的寄存器,并返回对模型的引用。

Linking through a string is useful (and sometimes required) when there is cyclic referencing. For example if you have two models A and B, and A refers to BandBthroughA(for example withForeignKeys), then one of the two models is defined first. This means that if you defineAfirst, it can not refer to theB` class itself, since at that point it does not yet exists. In Django, one then specifies the model through a string. The Django system will then first load the models, and then "tie the knot": resolve the references by replacing the strings with a reference to the actual model class.

当存在循环引用时,通过字符串链接是有用的(有时需要)。例如,如果您有两个模型A和B,而A指的是BandBthroughA(例如withForeignKeys),则首先定义两个模型中的一个。这意味着如果你定义了第一个,它就不能引用B`类本身,因为那时它还不存在。在Django中,然后通过字符串指定模型。然后Django系统首先加载模型,然后“打结”:通过用对实际模型类的引用替换字符串来解析引用。

#1


3  

Well you define models in the models.py file of an app. So that means that the module in which you stored the model class is app.models. Therefore the import reads:

那么你在应用程序的models.py文件中定义模型。这意味着存储模型类的模块是app.models。因此导入如下:

from app.models import MyModel

Django has in essence nothing to do with this: this is how Python loads modules and classes from these module(s).

Django本质上与此无关:这就是Python如何从这些模块加载模块和类。

Django however loads - when you for example run the server - the apps that are located in the INSTALLED_APPS list of the settings file (usually settings.py), and thus constructs a "register" where it stores Django models, and it names them in a uniform way: app_name.ModelName. There is no reason to specify models here, since models are defined in models.py, and it thus would only introduce "noise".

然而,当您运行服务器时,Django会加载位于设置文件的INSTALLED_APPS列表中的应用程序(通常是settings.py),从而构建一个存储Django模型的“寄存器”,并将其命名为统一的方式:app_name.ModelName。这里没有理由指定模型,因为模型是在models.py中定义的,因此它只会引入“噪声”。

You can obtain a reference to the model class with apps.get_model [Django-doc]

您可以使用apps.get_model [Django-doc]获取对模型类的引用

from django.apps import apps

apps.get_model('app_name', 'ModelName')

It thus then checks the registers of the loaded models, and returns a reference to the model.

然后,它检查加载的模型的寄存器,并返回对模型的引用。

Linking through a string is useful (and sometimes required) when there is cyclic referencing. For example if you have two models A and B, and A refers to BandBthroughA(for example withForeignKeys), then one of the two models is defined first. This means that if you defineAfirst, it can not refer to theB` class itself, since at that point it does not yet exists. In Django, one then specifies the model through a string. The Django system will then first load the models, and then "tie the knot": resolve the references by replacing the strings with a reference to the actual model class.

当存在循环引用时,通过字符串链接是有用的(有时需要)。例如,如果您有两个模型A和B,而A指的是BandBthroughA(例如withForeignKeys),则首先定义两个模型中的一个。这意味着如果你定义了第一个,它就不能引用B`类本身,因为那时它还不存在。在Django中,然后通过字符串指定模型。然后Django系统首先加载模型,然后“打结”:通过用对实际模型类的引用替换字符串来解析引用。