Django使用get_user_model和settings.AUTH_USER_MODEL

时间:2020-12-31 19:18:10

Reading the Django Documentation:

阅读Django文档:

get_user_model()

get_user_model()

Instead of referring to User directly, you should reference the user model using django.contrib.auth.get_user_model(). This method will return the currently active User model – the custom User model if one is specified, or User otherwise.

不要直接引用用户,您应该使用django.悔过书. authb .get_user_model()引用用户模型。此方法将返回当前活动的用户模型——如果指定了一个,则返回自定义用户模型,否则返回用户模型。

When you define a foreign key or many-to-many relations to the User model, you should specify the custom model using the AUTH_USER_MODEL setting.

在定义外键或多对多关系到用户模型时,应该使用AUTH_USER_MODEL设置指定自定义模型。

I'm confused with the above text. Should I be doing this:

我对上面的文字感到困惑。我应该这样做吗:

author = models.ForeignKey(settings.AUTH_USER_MODEL)

or this...

或者这个……

author = models.ForeignKey(get_user_model())

Both seem to work.

这两个似乎工作。

3 个解决方案

#1


43  

Using settings.AUTH_USER_MODEL will delay the retrieval of the actual model class until all apps are loaded. get_user_model will attempt to retrieve the model class at the moment your app is imported the first time.

使用设置。AUTH_USER_MODEL将延迟实际模型类的检索,直到加载所有应用程序。get_user_model将尝试在首次导入应用程序时检索模型类。

get_user_model cannot guarantee that the User model is already loaded into the app cache. It might work in your specific setup, but it is a hit-and-miss scenario. If you change some settings (e.g. the order of INSTALLED_APPS) it might very well break the import and you will have to spend additional time debugging.

get_user_model不能保证用户模型已经加载到应用程序缓存中。它可能在您的特定设置中工作,但这是一个“碰运气”的场景。如果您更改了一些设置(例如INSTALLED_APPS的顺序),很可能会中断导入,您将不得不花费更多时间调试。

settings.AUTH_USER_MODEL will pass a string as the foreign key model, and if the retrieval of the model class fails at the time this foreign key is imported, the retrieval will be delayed until all model classes are loaded into the cache.

设置。AUTH_USER_MODEL将传递一个字符串作为外键模型,如果在导入外键时模型类的检索失败,那么检索将被延迟,直到所有模型类都加载到缓存中。

#2


7  

New in Django 1.11.

Since Django 1.11 you can use get_user_model() in both cases. Or better: you should use it in both cases, as it is shorter and DRYer.

由于Django 1.11可以在这两种情况下使用get_user_model()。或者更好:你应该在这两种情况下使用它,因为它更短更干燥。

From the changelog:

从更新日志:

get_user_model() can now be called at import time, even in modules that define models.

现在可以在导入时调用get_user_model(),即使在定义模型的模块中也是如此。

https://docs.djangoproject.com/en/1.11/releases/1.11/#django-contrib-auth

https://docs.djangoproject.com/en/1.11/releases/1.11/ django-contrib-auth

Code example:

from django.db import models
from django.contrib.auth import get_user_model
...
    ...
    user = models.ForeignKey(
        get_user_model(),
        null=True, # explicitly set null, for easier upgrading to django 2.x. later, otherwise migrations will be incompatible
        ...
    )

#3


-5  

A way to fallback to the default user model if AUTH_USER_MODEL is not set:

如果AUTH_USER_MODEL没有设置,返回到默认用户模型的方法是:

from django.conf import settings
from django.contrib.auth.models import User

USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', User)

#1


43  

Using settings.AUTH_USER_MODEL will delay the retrieval of the actual model class until all apps are loaded. get_user_model will attempt to retrieve the model class at the moment your app is imported the first time.

使用设置。AUTH_USER_MODEL将延迟实际模型类的检索,直到加载所有应用程序。get_user_model将尝试在首次导入应用程序时检索模型类。

get_user_model cannot guarantee that the User model is already loaded into the app cache. It might work in your specific setup, but it is a hit-and-miss scenario. If you change some settings (e.g. the order of INSTALLED_APPS) it might very well break the import and you will have to spend additional time debugging.

get_user_model不能保证用户模型已经加载到应用程序缓存中。它可能在您的特定设置中工作,但这是一个“碰运气”的场景。如果您更改了一些设置(例如INSTALLED_APPS的顺序),很可能会中断导入,您将不得不花费更多时间调试。

settings.AUTH_USER_MODEL will pass a string as the foreign key model, and if the retrieval of the model class fails at the time this foreign key is imported, the retrieval will be delayed until all model classes are loaded into the cache.

设置。AUTH_USER_MODEL将传递一个字符串作为外键模型,如果在导入外键时模型类的检索失败,那么检索将被延迟,直到所有模型类都加载到缓存中。

#2


7  

New in Django 1.11.

Since Django 1.11 you can use get_user_model() in both cases. Or better: you should use it in both cases, as it is shorter and DRYer.

由于Django 1.11可以在这两种情况下使用get_user_model()。或者更好:你应该在这两种情况下使用它,因为它更短更干燥。

From the changelog:

从更新日志:

get_user_model() can now be called at import time, even in modules that define models.

现在可以在导入时调用get_user_model(),即使在定义模型的模块中也是如此。

https://docs.djangoproject.com/en/1.11/releases/1.11/#django-contrib-auth

https://docs.djangoproject.com/en/1.11/releases/1.11/ django-contrib-auth

Code example:

from django.db import models
from django.contrib.auth import get_user_model
...
    ...
    user = models.ForeignKey(
        get_user_model(),
        null=True, # explicitly set null, for easier upgrading to django 2.x. later, otherwise migrations will be incompatible
        ...
    )

#3


-5  

A way to fallback to the default user model if AUTH_USER_MODEL is not set:

如果AUTH_USER_MODEL没有设置,返回到默认用户模型的方法是:

from django.conf import settings
from django.contrib.auth.models import User

USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', User)