get_user_model()
[source]
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.
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. For example:
from django.conf import settings from django.db import models class Article(models.Model): author = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.CASCADE, )
When connecting to signals sent by the User
model, you should specify the custom model using the AUTH_USER_MODEL
setting. For example:
from django.conf import settings from django.db.models.signals import post_save def post_save_receiver(sender, instance, created, **kwargs): pass post_save.connect(post_save_receiver, sender=settings.AUTH_USER_MODEL)
Generally speaking, you should reference the User model with the AUTH_USER_MODEL
setting in code that is executed at import time. get_user_model()
only works once Django has imported all models.
Please login to continue.