{"title":"Customizing authentication in Django","version":"3.2","locale":"en","docname":"topics/auth/customizing","url":"/en/3.2/topics/auth/customizing/","canonical":"https://djangodocs.dev/en/3.2/topics/auth/customizing/","summary":"The authentication that comes with Django is good enough for most common cases, but you may have needs not met by the out-of-the-box defaults.","html":"<h1>Customizing authentication in Django<a class=\"heading-anchor\" href=\"#customizing-authentication-in-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>The authentication that comes with Django is good enough for most common cases,\nbut you may have needs not met by the out-of-the-box defaults. Customizing\nauthentication in your projects requires understanding what points of the\nprovided system are extensible or replaceable. This document provides details\nabout how the auth system can be customized.</p>\n<p><a class=\"reference internal\" href=\"#authentication-backends\"><span class=\"std std-ref\">Authentication backends</span></a> provide an extensible\nsystem for when a username and password stored with the user model need to be\nauthenticated against a different service than Django’s default.</p>\n<p>You can give your models <a class=\"reference internal\" href=\"#custom-permissions\"><span class=\"std std-ref\">custom permissions</span></a> that\ncan be checked through Django’s authorization system.</p>\n<p>You can <a class=\"reference internal\" href=\"#extending-user\"><span class=\"std std-ref\">extend</span></a> the default <code class=\"docutils literal notranslate\">User</code> model, or\n<a class=\"reference internal\" href=\"#auth-custom-user\"><span class=\"std std-ref\">substitute</span></a> a completely customized model.</p>\n<section id=\"other-authentication-sources\">\n<span id=\"authentication-backends\"></span><h2>Other authentication sources<a class=\"heading-anchor\" href=\"#other-authentication-sources\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>There may be times you have the need to hook into another authentication source\n– that is, another source of usernames and passwords or authentication\nmethods.</p>\n<p>For example, your company may already have an LDAP setup that stores a username\nand password for every employee. It’d be a hassle for both the network\nadministrator and the users themselves if users had separate accounts in LDAP\nand the Django-based applications.</p>\n<p>So, to handle situations like this, the Django authentication system lets you\nplug in other authentication sources. You can override Django’s default\ndatabase-based scheme, or you can use the default system in tandem with other\nsystems.</p>\n<p>See the <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#authentication-backends-reference\"><span class=\"std std-ref\">authentication backend reference</span></a> for information on the authentication\nbackends included with Django.</p>\n<section id=\"specifying-authentication-backends\">\n<h3>Specifying authentication backends<a class=\"heading-anchor\" href=\"#specifying-authentication-backends\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Behind the scenes, Django maintains a list of “authentication backends” that it\nchecks for authentication. When somebody calls\n<a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#django.contrib.auth.authenticate\" title=\"django.contrib.auth.authenticate\"><code class=\"xref py py-func docutils literal notranslate\">django.contrib.auth.authenticate()</code></a> – as described in <a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#how-to-log-a-user-in\"><span class=\"std std-ref\">How to log\na user in</span></a> – Django tries authenticating across\nall of its authentication backends. If the first authentication method fails,\nDjango tries the second one, and so on, until all backends have been attempted.</p>\n<p>The list of authentication backends to use is specified in the\n<a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-AUTHENTICATION_BACKENDS\"><code class=\"xref std std-setting docutils literal notranslate\">AUTHENTICATION_BACKENDS</code></a> setting. This should be a list of Python\npath names that point to Python classes that know how to authenticate. These\nclasses can be anywhere on your Python path.</p>\n<p>By default, <a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-AUTHENTICATION_BACKENDS\"><code class=\"xref std std-setting docutils literal notranslate\">AUTHENTICATION_BACKENDS</code></a> is set to:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"p\">[</span><span class=\"s1\">&#39;django.contrib.auth.backends.ModelBackend&#39;</span><span class=\"p\">]</span>\n</code></pre></div>\n<p>That’s the basic authentication backend that checks the Django users database\nand queries the built-in permissions. It does not provide protection against\nbrute force attacks via any rate limiting mechanism. You may either implement\nyour own rate limiting mechanism in a custom auth backend, or use the\nmechanisms provided by most Web servers.</p>\n<p>The order of <a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-AUTHENTICATION_BACKENDS\"><code class=\"xref std std-setting docutils literal notranslate\">AUTHENTICATION_BACKENDS</code></a> matters, so if the same\nusername and password is valid in multiple backends, Django will stop\nprocessing at the first positive match.</p>\n<p>If a backend raises a <a class=\"reference internal\" href=\"/en/3.2/ref/exceptions/#django.core.exceptions.PermissionDenied\" title=\"django.core.exceptions.PermissionDenied\"><code class=\"xref py py-class docutils literal notranslate\">PermissionDenied</code></a>\nexception, authentication will immediately fail. Django won’t check the\nbackends that follow.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>Once a user has authenticated, Django stores which backend was used to\nauthenticate the user in the user’s session, and re-uses the same backend\nfor the duration of that session whenever access to the currently\nauthenticated user is needed. This effectively means that authentication\nsources are cached on a per-session basis, so if you change\n<a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-AUTHENTICATION_BACKENDS\"><code class=\"xref std std-setting docutils literal notranslate\">AUTHENTICATION_BACKENDS</code></a>, you’ll need to clear out session data if\nyou need to force users to re-authenticate using different methods. A\nsimple way to do that is to execute <code class=\"docutils literal notranslate\">Session.objects.all().delete()</code>.</p>\n</aside>\n</section>\n<section id=\"writing-an-authentication-backend\">\n<h3>Writing an authentication backend<a class=\"heading-anchor\" href=\"#writing-an-authentication-backend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>An authentication backend is a class that implements two required methods:\n<code class=\"docutils literal notranslate\">get_user(user_id)</code> and <code class=\"docutils literal notranslate\">authenticate(request, **credentials)</code>, as well as\na set of optional permission related <a class=\"reference internal\" href=\"#authorization-methods\"><span class=\"std std-ref\">authorization methods</span></a>.</p>\n<p>The <code class=\"docutils literal notranslate\">get_user</code> method takes a <code class=\"docutils literal notranslate\">user_id</code> – which could be a username,\ndatabase ID or whatever, but has to be the primary key of your user object –\nand returns a user object or <code class=\"docutils literal notranslate\">None</code>.</p>\n<p>The <code class=\"docutils literal notranslate\">authenticate</code> method takes a <code class=\"docutils literal notranslate\">request</code> argument and credentials as\nkeyword arguments. Most of the time, it’ll look like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.backends</span> <span class=\"kn\">import</span> BaseBackend\n\n<span class=\"k\">class</span> <span class=\"nc\">MyBackend</span><span class=\"p\">(</span>BaseBackend<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">authenticate</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> request<span class=\"p\">,</span> username<span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">,</span> password<span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n        <span class=\"c1\"># Check the username/password and return a user.</span>\n        <span class=\"o\">...</span>\n</code></pre></div>\n<p>But it could also authenticate a token, like so:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.backends</span> <span class=\"kn\">import</span> BaseBackend\n\n<span class=\"k\">class</span> <span class=\"nc\">MyBackend</span><span class=\"p\">(</span>BaseBackend<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">authenticate</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> request<span class=\"p\">,</span> token<span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n        <span class=\"c1\"># Check the token and return a user.</span>\n        <span class=\"o\">...</span>\n</code></pre></div>\n<p>Either way, <code class=\"docutils literal notranslate\">authenticate()</code> should check the credentials it gets and return\na user object that matches those credentials if the credentials are valid. If\nthey’re not valid, it should return <code class=\"docutils literal notranslate\">None</code>.</p>\n<p><code class=\"docutils literal notranslate\">request</code> is an <a class=\"reference internal\" href=\"/en/3.2/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\">HttpRequest</code></a> and may be <code class=\"docutils literal notranslate\">None</code> if it\nwasn’t provided to <a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#django.contrib.auth.authenticate\" title=\"django.contrib.auth.authenticate\"><code class=\"xref py py-func docutils literal notranslate\">authenticate()</code></a> (which passes it\non to the backend).</p>\n<p>The Django admin is tightly coupled to the Django <a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#user-objects\"><span class=\"std std-ref\">User object</span></a>. The best way to deal with this is to create a Django <code class=\"docutils literal notranslate\">User</code>\nobject for each user that exists for your backend (e.g., in your LDAP\ndirectory, your external SQL database, etc.) You can either write a script to\ndo this in advance, or your <code class=\"docutils literal notranslate\">authenticate</code> method can do it the first time a\nuser logs in.</p>\n<p>Here’s an example backend that authenticates against a username and password\nvariable defined in your <code class=\"docutils literal notranslate\">settings.py</code> file and creates a Django <code class=\"docutils literal notranslate\">User</code>\nobject the first time a user authenticates:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.conf</span> <span class=\"kn\">import</span> settings\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.backends</span> <span class=\"kn\">import</span> BaseBackend\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.hashers</span> <span class=\"kn\">import</span> check_password\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.models</span> <span class=\"kn\">import</span> User\n\n<span class=\"k\">class</span> <span class=\"nc\">SettingsBackend</span><span class=\"p\">(</span>BaseBackend<span class=\"p\">):</span>\n    <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Authenticate against the settings ADMIN_LOGIN and ADMIN_PASSWORD.</span>\n\n<span class=\"sd\">    Use the login name and a hash of the password. For example:</span>\n\n<span class=\"sd\">    ADMIN_LOGIN = &#39;admin&#39;</span>\n<span class=\"sd\">    ADMIN_PASSWORD = &#39;pbkdf2_sha256$30000$Vo0VlMnkR4Bk$qEvtdyZRWTcOsCnI/oQ7fVOu1XAURIZYoOZ3iq8Dr4M=&#39;</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">authenticate</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> request<span class=\"p\">,</span> username<span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">,</span> password<span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n        login_valid <span class=\"o\">=</span> <span class=\"p\">(</span>settings<span class=\"o\">.</span>ADMIN_LOGIN <span class=\"o\">==</span> username<span class=\"p\">)</span>\n        pwd_valid <span class=\"o\">=</span> check_password<span class=\"p\">(</span>password<span class=\"p\">,</span> settings<span class=\"o\">.</span>ADMIN_PASSWORD<span class=\"p\">)</span>\n        <span class=\"k\">if</span> login_valid <span class=\"ow\">and</span> pwd_valid<span class=\"p\">:</span>\n            <span class=\"k\">try</span><span class=\"p\">:</span>\n                user <span class=\"o\">=</span> User<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>username<span class=\"o\">=</span>username<span class=\"p\">)</span>\n            <span class=\"k\">except</span> User<span class=\"o\">.</span>DoesNotExist<span class=\"p\">:</span>\n                <span class=\"c1\"># Create a new user. There&#39;s no need to set a password</span>\n                <span class=\"c1\"># because only the password from settings.py is checked.</span>\n                user <span class=\"o\">=</span> User<span class=\"p\">(</span>username<span class=\"o\">=</span>username<span class=\"p\">)</span>\n                user<span class=\"o\">.</span>is_staff <span class=\"o\">=</span> <span class=\"kc\">True</span>\n                user<span class=\"o\">.</span>is_superuser <span class=\"o\">=</span> <span class=\"kc\">True</span>\n                user<span class=\"o\">.</span>save<span class=\"p\">()</span>\n            <span class=\"k\">return</span> user\n        <span class=\"k\">return</span> <span class=\"kc\">None</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">get_user</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> user_id<span class=\"p\">):</span>\n        <span class=\"k\">try</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> User<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>pk<span class=\"o\">=</span>user_id<span class=\"p\">)</span>\n        <span class=\"k\">except</span> User<span class=\"o\">.</span>DoesNotExist<span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"kc\">None</span>\n</code></pre></div>\n</section>\n<section id=\"handling-authorization-in-custom-backends\">\n<span id=\"authorization-methods\"></span><h3>Handling authorization in custom backends<a class=\"heading-anchor\" href=\"#handling-authorization-in-custom-backends\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Custom auth backends can provide their own permissions.</p>\n<p>The user model and its manager will delegate permission lookup functions\n(<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.get_user_permissions\" title=\"django.contrib.auth.models.User.get_user_permissions\"><code class=\"xref py py-meth docutils literal notranslate\">get_user_permissions()</code></a>,\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.get_group_permissions\" title=\"django.contrib.auth.models.User.get_group_permissions\"><code class=\"xref py py-meth docutils literal notranslate\">get_group_permissions()</code></a>,\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.get_all_permissions\" title=\"django.contrib.auth.models.User.get_all_permissions\"><code class=\"xref py py-meth docutils literal notranslate\">get_all_permissions()</code></a>,\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.has_perm\" title=\"django.contrib.auth.models.User.has_perm\"><code class=\"xref py py-meth docutils literal notranslate\">has_perm()</code></a>,\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.has_module_perms\" title=\"django.contrib.auth.models.User.has_module_perms\"><code class=\"xref py py-meth docutils literal notranslate\">has_module_perms()</code></a>, and\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.UserManager.with_perm\" title=\"django.contrib.auth.models.UserManager.with_perm\"><code class=\"xref py py-meth docutils literal notranslate\">with_perm()</code></a>) to any\nauthentication backend that implements these functions.</p>\n<p>The permissions given to the user will be the superset of all permissions\nreturned by all backends. That is, Django grants a permission to a user that\nany one backend grants.</p>\n<p>If a backend raises a <a class=\"reference internal\" href=\"/en/3.2/ref/exceptions/#django.core.exceptions.PermissionDenied\" title=\"django.core.exceptions.PermissionDenied\"><code class=\"xref py py-class docutils literal notranslate\">PermissionDenied</code></a>\nexception in <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.has_perm\" title=\"django.contrib.auth.models.User.has_perm\"><code class=\"xref py py-meth docutils literal notranslate\">has_perm()</code></a> or\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.has_module_perms\" title=\"django.contrib.auth.models.User.has_module_perms\"><code class=\"xref py py-meth docutils literal notranslate\">has_module_perms()</code></a>, the authorization\nwill immediately fail and Django won’t check the backends that follow.</p>\n<p>A backend could implement permissions for the magic admin like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.backends</span> <span class=\"kn\">import</span> BaseBackend\n\n<span class=\"k\">class</span> <span class=\"nc\">MagicAdminBackend</span><span class=\"p\">(</span>BaseBackend<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">has_perm</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> user_obj<span class=\"p\">,</span> perm<span class=\"p\">,</span> obj<span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> user_obj<span class=\"o\">.</span>username <span class=\"o\">==</span> settings<span class=\"o\">.</span>ADMIN_LOGIN\n</code></pre></div>\n<p>This gives full permissions to the user granted access in the above example.\nNotice that in addition to the same arguments given to the associated\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">django.contrib.auth.models.User</code></a> functions, the backend auth functions\nall take the user object, which may be an anonymous user, as an argument.</p>\n<p>A full authorization implementation can be found in the <code class=\"docutils literal notranslate\">ModelBackend</code> class\nin <a class=\"extlink-source reference external\" href=\"https://github.com/django/django/blob/stable/3.2.x/django/contrib/auth/backends.py\">django/contrib/auth/backends.py</a>, which is the default backend and\nqueries the <code class=\"docutils literal notranslate\">auth_permission</code> table most of the time.</p>\n<section id=\"authorization-for-anonymous-users\">\n<span id=\"anonymous-auth\"></span><h4>Authorization for anonymous users<a class=\"heading-anchor\" href=\"#authorization-for-anonymous-users\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>An anonymous user is one that is not authenticated i.e. they have provided no\nvalid authentication details. However, that does not necessarily mean they are\nnot authorized to do anything. At the most basic level, most websites\nauthorize anonymous users to browse most of the site, and many allow anonymous\nposting of comments etc.</p>\n<p>Django’s permission framework does not have a place to store permissions for\nanonymous users. However, the user object passed to an authentication backend\nmay be an <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.AnonymousUser\" title=\"django.contrib.auth.models.AnonymousUser\"><code class=\"xref py py-class docutils literal notranslate\">django.contrib.auth.models.AnonymousUser</code></a> object, allowing\nthe backend to specify custom authorization behavior for anonymous users. This\nis especially useful for the authors of re-usable apps, who can delegate all\nquestions of authorization to the auth backend, rather than needing settings,\nfor example, to control anonymous access.</p>\n</section>\n<section id=\"authorization-for-inactive-users\">\n<span id=\"inactive-auth\"></span><h4>Authorization for inactive users<a class=\"heading-anchor\" href=\"#authorization-for-inactive-users\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>An inactive user is one that has its\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.is_active\" title=\"django.contrib.auth.models.User.is_active\"><code class=\"xref py py-attr docutils literal notranslate\">is_active</code></a> field set to <code class=\"docutils literal notranslate\">False</code>. The\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.backends.ModelBackend\" title=\"django.contrib.auth.backends.ModelBackend\"><code class=\"xref py py-class docutils literal notranslate\">ModelBackend</code></a> and\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.backends.RemoteUserBackend\" title=\"django.contrib.auth.backends.RemoteUserBackend\"><code class=\"xref py py-class docutils literal notranslate\">RemoteUserBackend</code></a> authentication\nbackends prohibits these users from authenticating. If a custom user model\ndoesn’t have an <a class=\"reference internal\" href=\"#django.contrib.auth.models.CustomUser.is_active\" title=\"django.contrib.auth.models.CustomUser.is_active\"><code class=\"xref py py-attr docutils literal notranslate\">is_active</code></a> field,\nall users will be allowed to authenticate.</p>\n<p>You can use <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.backends.AllowAllUsersModelBackend\" title=\"django.contrib.auth.backends.AllowAllUsersModelBackend\"><code class=\"xref py py-class docutils literal notranslate\">AllowAllUsersModelBackend</code></a>\nor <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.backends.AllowAllUsersRemoteUserBackend\" title=\"django.contrib.auth.backends.AllowAllUsersRemoteUserBackend\"><code class=\"xref py py-class docutils literal notranslate\">AllowAllUsersRemoteUserBackend</code></a> if you\nwant to allow inactive users to authenticate.</p>\n<p>The support for anonymous users in the permission system allows for a scenario\nwhere anonymous users have permissions to do something while inactive\nauthenticated users do not.</p>\n<p>Do not forget to test for the <code class=\"docutils literal notranslate\">is_active</code> attribute of the user in your own\nbackend permission methods.</p>\n</section>\n<section id=\"handling-object-permissions\">\n<h4>Handling object permissions<a class=\"heading-anchor\" href=\"#handling-object-permissions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Django’s permission framework has a foundation for object permissions, though\nthere is no implementation for it in the core. That means that checking for\nobject permissions will always return <code class=\"docutils literal notranslate\">False</code> or an empty list (depending on\nthe check performed). An authentication backend will receive the keyword\nparameters <code class=\"docutils literal notranslate\">obj</code> and <code class=\"docutils literal notranslate\">user_obj</code> for each object related authorization\nmethod and can return the object level permission as appropriate.</p>\n</section>\n</section>\n</section>\n<section id=\"custom-permissions\">\n<span id=\"id1\"></span><h2>Custom permissions<a class=\"heading-anchor\" href=\"#custom-permissions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>To create custom permissions for a given model object, use the <code class=\"docutils literal notranslate\">permissions</code>\n<a class=\"reference internal\" href=\"/en/3.2/topics/db/models/#meta-options\"><span class=\"std std-ref\">model Meta attribute</span></a>.</p>\n<p>This example <code class=\"docutils literal notranslate\">Task</code> model creates two custom permissions, i.e., actions users\ncan or cannot do with <code class=\"docutils literal notranslate\">Task</code> instances, specific to your application:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span> <span class=\"nc\">Task</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    <span class=\"o\">...</span>\n    <span class=\"k\">class</span> <span class=\"nc\">Meta</span><span class=\"p\">:</span>\n        permissions <span class=\"o\">=</span> <span class=\"p\">[</span>\n            <span class=\"p\">(</span><span class=\"s2\">&quot;change_task_status&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Can change the status of tasks&quot;</span><span class=\"p\">),</span>\n            <span class=\"p\">(</span><span class=\"s2\">&quot;close_task&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Can remove a task by setting its status as closed&quot;</span><span class=\"p\">),</span>\n        <span class=\"p\">]</span>\n</code></pre></div>\n<p>The only thing this does is create those extra permissions when you run\n<a class=\"reference internal\" href=\"/en/3.2/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">manage.py migrate</code></a> (the function that creates permissions\nis connected to the <a class=\"reference internal\" href=\"/en/3.2/ref/signals/#django.db.models.signals.post_migrate\" title=\"django.db.models.signals.post_migrate\"><code class=\"xref py py-data docutils literal notranslate\">post_migrate</code></a> signal).\nYour code is in charge of checking the value of these permissions when a user\nis trying to access the functionality provided by the application (changing the\nstatus of tasks or closing tasks.) Continuing the above example, the following\nchecks if a user may close tasks:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>user<span class=\"o\">.</span>has_perm<span class=\"p\">(</span><span class=\"s1\">&#39;app.close_task&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n<section id=\"extending-the-existing-user-model\">\n<span id=\"extending-user\"></span><h2>Extending the existing <code class=\"docutils literal notranslate\">User</code> model<a class=\"heading-anchor\" href=\"#extending-the-existing-user-model\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>There are two ways to extend the default\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a> model without substituting your own\nmodel. If the changes you need are purely behavioral, and don’t require any\nchange to what is stored in the database, you can create a <a class=\"reference internal\" href=\"/en/3.2/topics/db/models/#proxy-models\"><span class=\"std std-ref\">proxy model</span></a> based on <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a>. This\nallows for any of the features offered by proxy models including default\nordering, custom managers, or custom model methods.</p>\n<p>If you wish to store information related to <code class=\"docutils literal notranslate\">User</code>, you can use a\n<a class=\"reference internal\" href=\"/en/3.2/ref/models/fields/#django.db.models.OneToOneField\" title=\"django.db.models.OneToOneField\"><code class=\"xref py py-class docutils literal notranslate\">OneToOneField</code></a> to a model containing the fields for\nadditional information. This one-to-one model is often called a profile model,\nas it might store non-auth related information about a site user. For example\nyou might create an Employee model:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.models</span> <span class=\"kn\">import</span> User\n\n<span class=\"k\">class</span> <span class=\"nc\">Employee</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    user <span class=\"o\">=</span> models<span class=\"o\">.</span>OneToOneField<span class=\"p\">(</span>User<span class=\"p\">,</span> on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">)</span>\n    department <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">100</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Assuming an existing Employee Fred Smith who has both a User and Employee\nmodel, you can access the related information using Django’s standard related\nmodel conventions:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>u <span class=\"o\">=</span> User<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>username<span class=\"o\">=</span><span class=\"s1\">&#39;fsmith&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>freds_department <span class=\"o\">=</span> u<span class=\"o\">.</span>employee<span class=\"o\">.</span>department\n</code></pre></div>\n<p>To add a profile model’s fields to the user page in the admin, define an\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin\" title=\"django.contrib.admin.InlineModelAdmin\"><code class=\"xref py py-class docutils literal notranslate\">InlineModelAdmin</code></a> (for this example, we’ll use a\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/admin/#django.contrib.admin.StackedInline\" title=\"django.contrib.admin.StackedInline\"><code class=\"xref py py-class docutils literal notranslate\">StackedInline</code></a>) in your app’s <code class=\"docutils literal notranslate\">admin.py</code> and\nadd it to a <code class=\"docutils literal notranslate\">UserAdmin</code> class which is registered with the\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a> class:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib</span> <span class=\"kn\">import</span> admin\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.admin</span> <span class=\"kn\">import</span> UserAdmin <span class=\"k\">as</span> BaseUserAdmin\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.models</span> <span class=\"kn\">import</span> User\n\n<span class=\"kn\">from</span> <span class=\"nn\">my_user_profile_app.models</span> <span class=\"kn\">import</span> Employee\n\n<span class=\"c1\"># Define an inline admin descriptor for Employee model</span>\n<span class=\"c1\"># which acts a bit like a singleton</span>\n<span class=\"k\">class</span> <span class=\"nc\">EmployeeInline</span><span class=\"p\">(</span>admin<span class=\"o\">.</span>StackedInline<span class=\"p\">):</span>\n    model <span class=\"o\">=</span> Employee\n    can_delete <span class=\"o\">=</span> <span class=\"kc\">False</span>\n    verbose_name_plural <span class=\"o\">=</span> <span class=\"s1\">&#39;employee&#39;</span>\n\n<span class=\"c1\"># Define a new User admin</span>\n<span class=\"k\">class</span> <span class=\"nc\">UserAdmin</span><span class=\"p\">(</span>BaseUserAdmin<span class=\"p\">):</span>\n    inlines <span class=\"o\">=</span> <span class=\"p\">(</span>EmployeeInline<span class=\"p\">,)</span>\n\n<span class=\"c1\"># Re-register UserAdmin</span>\nadmin<span class=\"o\">.</span>site<span class=\"o\">.</span>unregister<span class=\"p\">(</span>User<span class=\"p\">)</span>\nadmin<span class=\"o\">.</span>site<span class=\"o\">.</span>register<span class=\"p\">(</span>User<span class=\"p\">,</span> UserAdmin<span class=\"p\">)</span>\n</code></pre></div>\n<p>These profile models are not special in any way - they are just Django models\nthat happen to have a one-to-one link with a user model. As such, they aren’t\nauto created when a user is created, but\na <a class=\"reference internal\" href=\"/en/3.2/ref/signals/#django.db.models.signals.post_save\" title=\"django.db.models.signals.post_save\"><code class=\"xref py py-attr docutils literal notranslate\">django.db.models.signals.post_save</code></a> could be used to create or update\nrelated models as appropriate.</p>\n<p>Using related models results in additional queries or joins to retrieve the\nrelated data. Depending on your needs, a custom user model that includes the\nrelated fields may be your better option, however, existing relations to the\ndefault user model within your project’s apps may justify the extra database\nload.</p>\n</section>\n<section id=\"substituting-a-custom-user-model\">\n<span id=\"auth-custom-user\"></span><h2>Substituting a custom <code class=\"docutils literal notranslate\">User</code> model<a class=\"heading-anchor\" href=\"#substituting-a-custom-user-model\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Some kinds of projects may have authentication requirements for which Django’s\nbuilt-in <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a> model is not always\nappropriate. For instance, on some sites it makes more sense to use an email\naddress as your identification token instead of a username.</p>\n<p>Django allows you to override the default user model by providing a value for\nthe <a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-AUTH_USER_MODEL\"><code class=\"xref std std-setting docutils literal notranslate\">AUTH_USER_MODEL</code></a> setting that references a custom model:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>AUTH_USER_MODEL <span class=\"o\">=</span> <span class=\"s1\">&#39;myapp.MyUser&#39;</span>\n</code></pre></div>\n<p>This dotted pair describes the name of the Django app (which must be in your\n<a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a>), and the name of the Django model that you wish to\nuse as your user model.</p>\n<section id=\"using-a-custom-user-model-when-starting-a-project\">\n<h3>Using a custom user model when starting a project<a class=\"heading-anchor\" href=\"#using-a-custom-user-model-when-starting-a-project\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you’re starting a new project, it’s highly recommended to set up a custom\nuser model, even if the default <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a> model\nis sufficient for you. This model behaves identically to the default user\nmodel, but you’ll be able to customize it in the future if the need arises:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.models</span> <span class=\"kn\">import</span> AbstractUser\n\n<span class=\"k\">class</span> <span class=\"nc\">User</span><span class=\"p\">(</span>AbstractUser<span class=\"p\">):</span>\n    <span class=\"k\">pass</span>\n</code></pre></div>\n<p>Don’t forget to point <a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-AUTH_USER_MODEL\"><code class=\"xref std std-setting docutils literal notranslate\">AUTH_USER_MODEL</code></a> to it. Do this before creating\nany migrations or running <code class=\"docutils literal notranslate\">manage.py migrate</code> for the first time.</p>\n<p>Also, register the model in the app’s <code class=\"docutils literal notranslate\">admin.py</code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib</span> <span class=\"kn\">import</span> admin\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.admin</span> <span class=\"kn\">import</span> UserAdmin\n<span class=\"kn\">from</span> <span class=\"nn\">.models</span> <span class=\"kn\">import</span> User\n\nadmin<span class=\"o\">.</span>site<span class=\"o\">.</span>register<span class=\"p\">(</span>User<span class=\"p\">,</span> UserAdmin<span class=\"p\">)</span>\n</code></pre></div>\n</section>\n<section id=\"changing-to-a-custom-user-model-mid-project\">\n<h3>Changing to a custom user model mid-project<a class=\"heading-anchor\" href=\"#changing-to-a-custom-user-model-mid-project\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Changing <a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-AUTH_USER_MODEL\"><code class=\"xref std std-setting docutils literal notranslate\">AUTH_USER_MODEL</code></a> after you’ve created database tables is\nsignificantly more difficult since it affects foreign keys and many-to-many\nrelationships, for example.</p>\n<p>This change can’t be done automatically and requires manually fixing your\nschema, moving your data from the old user table, and possibly manually\nreapplying some migrations. See <a class=\"extlink-ticket reference external\" href=\"https://code.djangoproject.com/ticket/25313\">#25313</a> for an outline of the steps.</p>\n<p>Due to limitations of Django’s dynamic dependency feature for swappable\nmodels, the model referenced by <a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-AUTH_USER_MODEL\"><code class=\"xref std std-setting docutils literal notranslate\">AUTH_USER_MODEL</code></a> must be created in\nthe first migration of its app (usually called <code class=\"docutils literal notranslate\">0001_initial</code>); otherwise,\nyou’ll have dependency issues.</p>\n<p>In addition, you may run into a <code class=\"docutils literal notranslate\">CircularDependencyError</code> when running your\nmigrations as Django won’t be able to automatically break the dependency loop\ndue to the dynamic dependency. If you see this error, you should break the loop\nby moving the models depended on by your user model into a second migration.\n(You can try making two normal models that have a <code class=\"docutils literal notranslate\">ForeignKey</code> to each other\nand seeing how <code class=\"docutils literal notranslate\">makemigrations</code> resolves that circular dependency if you want\nto see how it’s usually done.)</p>\n</section>\n<section id=\"reusable-apps-and-auth-user-model\">\n<h3>Reusable apps and <code class=\"docutils literal notranslate\">AUTH_USER_MODEL</code><a class=\"heading-anchor\" href=\"#reusable-apps-and-auth-user-model\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Reusable apps shouldn’t implement a custom user model. A project may use many\napps, and two reusable apps that implemented a custom user model couldn’t be\nused together. If you need to store per user information in your app, use\na <a class=\"reference internal\" href=\"/en/3.2/ref/models/fields/#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> or\n<a class=\"reference internal\" href=\"/en/3.2/ref/models/fields/#django.db.models.OneToOneField\" title=\"django.db.models.OneToOneField\"><code class=\"xref py py-class docutils literal notranslate\">OneToOneField</code></a> to <code class=\"docutils literal notranslate\">settings.AUTH_USER_MODEL</code>\nas described below.</p>\n</section>\n<section id=\"referencing-the-user-model\">\n<h3>Referencing the <code class=\"docutils literal notranslate\">User</code> model<a class=\"heading-anchor\" href=\"#referencing-the-user-model\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you reference <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a> directly (for\nexample, by referring to it in a foreign key), your code will not work in\nprojects where the <a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-AUTH_USER_MODEL\"><code class=\"xref std std-setting docutils literal notranslate\">AUTH_USER_MODEL</code></a> setting has been changed to a\ndifferent user model.</p>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.get_user_model\">\n<span class=\"sig-name descname\">get_user_model</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.get_user_model\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Instead of referring to <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a> directly,\nyou should reference the user model using\n<code class=\"docutils literal notranslate\">django.contrib.auth.get_user_model()</code>. This method will return the\ncurrently active user model – the custom user model if one is specified, or\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a> otherwise.</p>\n<p>When you define a foreign key or many-to-many relations to the user model,\nyou should specify the custom model using the <a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-AUTH_USER_MODEL\"><code class=\"xref std std-setting docutils literal notranslate\">AUTH_USER_MODEL</code></a>\nsetting. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.conf</span> <span class=\"kn\">import</span> settings\n<span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">class</span> <span class=\"nc\">Article</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    author <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>\n        settings<span class=\"o\">.</span>AUTH_USER_MODEL<span class=\"p\">,</span>\n        on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">,</span>\n    <span class=\"p\">)</span>\n</code></pre></div>\n<p>When connecting to signals sent by the user model, you should specify\nthe custom model using the <a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-AUTH_USER_MODEL\"><code class=\"xref std std-setting docutils literal notranslate\">AUTH_USER_MODEL</code></a> setting. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.conf</span> <span class=\"kn\">import</span> settings\n<span class=\"kn\">from</span> <span class=\"nn\">django.db.models.signals</span> <span class=\"kn\">import</span> post_save\n\n<span class=\"k\">def</span> <span class=\"nf\">post_save_receiver</span><span class=\"p\">(</span>sender<span class=\"p\">,</span> instance<span class=\"p\">,</span> created<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n    <span class=\"k\">pass</span>\n\npost_save<span class=\"o\">.</span>connect<span class=\"p\">(</span>post_save_receiver<span class=\"p\">,</span> sender<span class=\"o\">=</span>settings<span class=\"o\">.</span>AUTH_USER_MODEL<span class=\"p\">)</span>\n</code></pre></div>\n<p>Generally speaking, it’s easiest to refer to the user model with the\n<a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-AUTH_USER_MODEL\"><code class=\"xref std std-setting docutils literal notranslate\">AUTH_USER_MODEL</code></a> setting in code that’s executed at import time,\nhowever, it’s also possible to call <code class=\"docutils literal notranslate\">get_user_model()</code> while Django\nis importing models, so you could use\n<code class=\"docutils literal notranslate\">models.ForeignKey(get_user_model(), ...)</code>.</p>\n<p>If your app is tested with multiple user models, using\n<code class=\"docutils literal notranslate\">&#64;override_settings(AUTH_USER_MODEL=...)</code> for example, and you cache the\nresult of <code class=\"docutils literal notranslate\">get_user_model()</code> in a module-level variable, you may need to\nlisten to the  <a class=\"reference internal\" href=\"/en/3.2/ref/signals/#django.test.signals.setting_changed\" title=\"django.test.signals.setting_changed\"><code class=\"xref py py-data docutils literal notranslate\">setting_changed</code></a> signal to clear\nthe cache. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.apps</span> <span class=\"kn\">import</span> apps\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth</span> <span class=\"kn\">import</span> get_user_model\n<span class=\"kn\">from</span> <span class=\"nn\">django.core.signals</span> <span class=\"kn\">import</span> setting_changed\n<span class=\"kn\">from</span> <span class=\"nn\">django.dispatch</span> <span class=\"kn\">import</span> receiver\n\n<span class=\"nd\">@receiver</span><span class=\"p\">(</span>setting_changed<span class=\"p\">)</span>\n<span class=\"k\">def</span> <span class=\"nf\">user_model_swapped</span><span class=\"p\">(</span><span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n    <span class=\"k\">if</span> kwargs<span class=\"p\">[</span><span class=\"s1\">&#39;setting&#39;</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;AUTH_USER_MODEL&#39;</span><span class=\"p\">:</span>\n        apps<span class=\"o\">.</span>clear_cache<span class=\"p\">()</span>\n        <span class=\"kn\">from</span> <span class=\"nn\">myapp</span> <span class=\"kn\">import</span> some_module\n        some_module<span class=\"o\">.</span>UserModel <span class=\"o\">=</span> get_user_model<span class=\"p\">()</span>\n</code></pre></div>\n</dd></dl>\n\n</section>\n<section id=\"specifying-a-custom-user-model\">\n<span id=\"specifying-custom-user-model\"></span><h3>Specifying a custom user model<a class=\"heading-anchor\" href=\"#specifying-a-custom-user-model\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When you start your project with a custom user model, stop to consider if this\nis the right choice for your project.</p>\n<p>Keeping all user related information in one model removes the need for\nadditional or more complex database queries to retrieve related models. On the\nother hand, it may be more suitable to store app-specific user information in a\nmodel that has a relation with your custom user model. That allows each app to\nspecify its own user data requirements without potentially conflicting or\nbreaking assumptions by other apps. It also means that you would keep your user\nmodel as simple as possible, focused on authentication, and following the\nminimum requirements Django expects custom user models to meet.</p>\n<p>If you use the default authentication backend, then your model must have a\nsingle unique field that can be used for identification purposes. This can\nbe a username, an email address, or any other unique attribute. A non-unique\nusername field is allowed if you use a custom authentication backend that\ncan support it.</p>\n<p>The easiest way to construct a compliant custom user model is to inherit from\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser\" title=\"django.contrib.auth.models.AbstractBaseUser\"><code class=\"xref py py-class docutils literal notranslate\">AbstractBaseUser</code></a>.\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser\" title=\"django.contrib.auth.models.AbstractBaseUser\"><code class=\"xref py py-class docutils literal notranslate\">AbstractBaseUser</code></a> provides the core\nimplementation of a user model, including hashed passwords and tokenized\npassword resets. You must then provide some key implementation details:</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.CustomUser\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">models.</span><span class=\"sig-name descname\">CustomUser</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.CustomUser\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.CustomUser.USERNAME_FIELD\">\n<span class=\"sig-name descname\">USERNAME_FIELD</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.CustomUser.USERNAME_FIELD\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A string describing the name of the field on the user model that is\nused as the unique identifier. This will usually be a username of some\nkind, but it can also be an email address, or any other unique\nidentifier. The field <em>must</em> be unique (i.e., have <code class=\"docutils literal notranslate\">unique=True</code> set\nin its definition), unless you use a custom authentication backend that\ncan support non-unique usernames.</p>\n<p>In the following example, the field <code class=\"docutils literal notranslate\">identifier</code> is used\nas the identifying field:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span> <span class=\"nc\">MyUser</span><span class=\"p\">(</span>AbstractBaseUser<span class=\"p\">):</span>\n    identifier <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">40</span><span class=\"p\">,</span> unique<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n    <span class=\"o\">...</span>\n    USERNAME_FIELD <span class=\"o\">=</span> <span class=\"s1\">&#39;identifier&#39;</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.CustomUser.EMAIL_FIELD\">\n<span class=\"sig-name descname\">EMAIL_FIELD</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.CustomUser.EMAIL_FIELD\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A string describing the name of the email field on the <code class=\"docutils literal notranslate\">User</code> model.\nThis value is returned by\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser.get_email_field_name\" title=\"django.contrib.auth.models.AbstractBaseUser.get_email_field_name\"><code class=\"xref py py-meth docutils literal notranslate\">get_email_field_name()</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.CustomUser.REQUIRED_FIELDS\">\n<span class=\"sig-name descname\">REQUIRED_FIELDS</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.CustomUser.REQUIRED_FIELDS\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A list of the field names that will be prompted for when creating a\nuser via the <a class=\"reference internal\" href=\"/en/3.2/ref/django-admin/#django-admin-createsuperuser\"><code class=\"xref std std-djadmin docutils literal notranslate\">createsuperuser</code></a> management command. The user\nwill be prompted to supply a value for each of these fields. It must\ninclude any field for which <a class=\"reference internal\" href=\"/en/3.2/ref/models/fields/#django.db.models.Field.blank\" title=\"django.db.models.Field.blank\"><code class=\"xref py py-attr docutils literal notranslate\">blank</code></a> is\n<code class=\"docutils literal notranslate\">False</code> or undefined and may include additional fields you want\nprompted for when a user is created interactively.\n<code class=\"docutils literal notranslate\">REQUIRED_FIELDS</code> has no effect in other parts of Django, like\ncreating a user in the admin.</p>\n<p>For example, here is the partial definition for a user model that\ndefines two required fields - a date of birth and height:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span> <span class=\"nc\">MyUser</span><span class=\"p\">(</span>AbstractBaseUser<span class=\"p\">):</span>\n    <span class=\"o\">...</span>\n    date_of_birth <span class=\"o\">=</span> models<span class=\"o\">.</span>DateField<span class=\"p\">()</span>\n    height <span class=\"o\">=</span> models<span class=\"o\">.</span>FloatField<span class=\"p\">()</span>\n    <span class=\"o\">...</span>\n    REQUIRED_FIELDS <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;date_of_birth&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;height&#39;</span><span class=\"p\">]</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p><code class=\"docutils literal notranslate\">REQUIRED_FIELDS</code> must contain all required fields on your user\nmodel, but should <em>not</em> contain the <code class=\"docutils literal notranslate\">USERNAME_FIELD</code> or\n<code class=\"docutils literal notranslate\">password</code> as these fields will always be prompted for.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.CustomUser.is_active\">\n<span class=\"sig-name descname\">is_active</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.CustomUser.is_active\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A boolean attribute that indicates whether the user is considered\n“active”.  This attribute is provided as an attribute on\n<code class=\"docutils literal notranslate\">AbstractBaseUser</code> defaulting to <code class=\"docutils literal notranslate\">True</code>. How you choose to\nimplement it will depend on the details of your chosen auth backends.\nSee the documentation of the <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.is_active\" title=\"django.contrib.auth.models.User.is_active\"><code class=\"xref py py-attr docutils literal notranslate\">is_active attribute on the built-in\nuser model</code></a> for details.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.CustomUser.get_full_name\">\n<span class=\"sig-name descname\">get_full_name</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.CustomUser.get_full_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Optional. A longer formal identifier for the user such as their full\nname. If implemented, this appears alongside the username in an\nobject’s history in <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/admin/#module-django.contrib.admin\" title=\"django.contrib.admin: Django's admin site.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.admin</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.CustomUser.get_short_name\">\n<span class=\"sig-name descname\">get_short_name</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.CustomUser.get_short_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Optional. A short, informal identifier for the user such as their\nfirst name. If implemented, this replaces the username in the greeting\nto the user in the header of <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/admin/#module-django.contrib.admin\" title=\"django.contrib.admin: Django's admin site.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.admin</code></a>.</p>\n</dd></dl>\n\n<aside class=\"admonition-importing-abstractbaseuser admonition\" role=\"note\">\n<p class=\"admonition-title\">Importing <code class=\"docutils literal notranslate\">AbstractBaseUser</code></p>\n<p><code class=\"docutils literal notranslate\">AbstractBaseUser</code> and <code class=\"docutils literal notranslate\">BaseUserManager</code> are importable from\n<code class=\"docutils literal notranslate\">django.contrib.auth.base_user</code> so that they can be imported without\nincluding <code class=\"docutils literal notranslate\">django.contrib.auth</code> in <a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a>.</p>\n</aside>\n</dd></dl>\n\n<p>The following attributes and methods are available on any subclass of\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser\" title=\"django.contrib.auth.models.AbstractBaseUser\"><code class=\"xref py py-class docutils literal notranslate\">AbstractBaseUser</code></a>:</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractBaseUser\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">models.</span><span class=\"sig-name descname\">AbstractBaseUser</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractBaseUser\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractBaseUser.get_username\">\n<span class=\"sig-name descname\">get_username</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractBaseUser.get_username\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the value of the field nominated by <code class=\"docutils literal notranslate\">USERNAME_FIELD</code>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractBaseUser.clean\">\n<span class=\"sig-name descname\">clean</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractBaseUser.clean\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Normalizes the username by calling <a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser.normalize_username\" title=\"django.contrib.auth.models.AbstractBaseUser.normalize_username\"><code class=\"xref py py-meth docutils literal notranslate\">normalize_username()</code></a>. If you\noverride this method, be sure to call <code class=\"docutils literal notranslate\">super()</code> to retain the\nnormalization.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractBaseUser.get_email_field_name\">\n<em class=\"property\"><span class=\"k\">classmethod</span> </em><span class=\"sig-name descname\">get_email_field_name</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractBaseUser.get_email_field_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the name of the email field specified by the\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.CustomUser.EMAIL_FIELD\" title=\"django.contrib.auth.models.CustomUser.EMAIL_FIELD\"><code class=\"xref py py-attr docutils literal notranslate\">EMAIL_FIELD</code></a> attribute. Defaults to\n<code class=\"docutils literal notranslate\">'email'</code> if <code class=\"docutils literal notranslate\">EMAIL_FIELD</code> isn’t specified.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractBaseUser.normalize_username\">\n<em class=\"property\"><span class=\"k\">classmethod</span> </em><span class=\"sig-name descname\">normalize_username</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">username</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractBaseUser.normalize_username\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Applies NFKC Unicode normalization to usernames so that visually\nidentical characters with different Unicode code points are considered\nidentical.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractBaseUser.is_authenticated\">\n<span class=\"sig-name descname\">is_authenticated</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractBaseUser.is_authenticated\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Read-only attribute which is always <code class=\"docutils literal notranslate\">True</code> (as opposed to\n<code class=\"docutils literal notranslate\">AnonymousUser.is_authenticated</code> which is always <code class=\"docutils literal notranslate\">False</code>).\nThis is a way to tell if the user has been authenticated. This does not\nimply any permissions and doesn’t check if the user is active or has\na valid session. Even though normally you will check this attribute on\n<code class=\"docutils literal notranslate\">request.user</code> to find out whether it has been populated by the\n<a class=\"reference internal\" href=\"/en/3.2/ref/middleware/#django.contrib.auth.middleware.AuthenticationMiddleware\" title=\"django.contrib.auth.middleware.AuthenticationMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">AuthenticationMiddleware</code></a>\n(representing the currently logged-in user), you should know this\nattribute is <code class=\"docutils literal notranslate\">True</code> for any <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a> instance.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractBaseUser.is_anonymous\">\n<span class=\"sig-name descname\">is_anonymous</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractBaseUser.is_anonymous\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Read-only attribute which is always <code class=\"docutils literal notranslate\">False</code>. This is a way of\ndifferentiating <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a> and <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.AnonymousUser\" title=\"django.contrib.auth.models.AnonymousUser\"><code class=\"xref py py-class docutils literal notranslate\">AnonymousUser</code></a>\nobjects. Generally, you should prefer using\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.is_authenticated\" title=\"django.contrib.auth.models.User.is_authenticated\"><code class=\"xref py py-attr docutils literal notranslate\">is_authenticated</code></a> to this attribute.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractBaseUser.set_password\">\n<span class=\"sig-name descname\">set_password</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">raw_password</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractBaseUser.set_password\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Sets the user’s password to the given raw string, taking care of the\npassword hashing. Doesn’t save the\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser\" title=\"django.contrib.auth.models.AbstractBaseUser\"><code class=\"xref py py-class docutils literal notranslate\">AbstractBaseUser</code></a> object.</p>\n<p>When the raw_password is <code class=\"docutils literal notranslate\">None</code>, the password will be set to an\nunusable password, as if\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser.set_unusable_password\" title=\"django.contrib.auth.models.AbstractBaseUser.set_unusable_password\"><code class=\"xref py py-meth docutils literal notranslate\">set_unusable_password()</code></a>\nwere used.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractBaseUser.check_password\">\n<span class=\"sig-name descname\">check_password</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">raw_password</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractBaseUser.check_password\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\">True</code> if the given raw string is the correct password for\nthe user. (This takes care of the password hashing in making the\ncomparison.)</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractBaseUser.set_unusable_password\">\n<span class=\"sig-name descname\">set_unusable_password</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractBaseUser.set_unusable_password\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Marks the user as having no password set.  This isn’t the same as\nhaving a blank string for a password.\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser.check_password\" title=\"django.contrib.auth.models.AbstractBaseUser.check_password\"><code class=\"xref py py-meth docutils literal notranslate\">check_password()</code></a> for this user\nwill never return <code class=\"docutils literal notranslate\">True</code>. Doesn’t save the\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser\" title=\"django.contrib.auth.models.AbstractBaseUser\"><code class=\"xref py py-class docutils literal notranslate\">AbstractBaseUser</code></a> object.</p>\n<p>You may need this if authentication for your application takes place\nagainst an existing external source such as an LDAP directory.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractBaseUser.has_usable_password\">\n<span class=\"sig-name descname\">has_usable_password</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractBaseUser.has_usable_password\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\">False</code> if\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser.set_unusable_password\" title=\"django.contrib.auth.models.AbstractBaseUser.set_unusable_password\"><code class=\"xref py py-meth docutils literal notranslate\">set_unusable_password()</code></a> has\nbeen called for this user.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash\">\n<span class=\"sig-name descname\">get_session_auth_hash</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns an HMAC of the password field. Used for\n<a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#session-invalidation-on-password-change\"><span class=\"std std-ref\">Session invalidation on password change</span></a>.</p>\n<aside class=\"version-note version-changed\" data-version=\"3.1\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 3.1</p><p>The hashing algorithm was changed to the SHA-256.</p>\n</aside>\n</dd></dl>\n\n</dd></dl>\n\n<p><a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractUser\" title=\"django.contrib.auth.models.AbstractUser\"><code class=\"xref py py-class docutils literal notranslate\">AbstractUser</code></a> subclasses <a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser\" title=\"django.contrib.auth.models.AbstractBaseUser\"><code class=\"xref py py-class docutils literal notranslate\">AbstractBaseUser</code></a>:</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractUser\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">models.</span><span class=\"sig-name descname\">AbstractUser</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractUser\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.AbstractUser.clean\">\n<span class=\"sig-name descname\">clean</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.AbstractUser.clean\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Normalizes the email by calling\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.BaseUserManager.normalize_email\" title=\"django.contrib.auth.models.BaseUserManager.normalize_email\"><code class=\"xref py py-meth docutils literal notranslate\">BaseUserManager.normalize_email()</code></a>. If you override this method,\nbe sure to call <code class=\"docutils literal notranslate\">super()</code> to retain the normalization.</p>\n</dd></dl>\n\n</dd></dl>\n\n</section>\n<section id=\"writing-a-manager-for-a-custom-user-model\">\n<h3>Writing a manager for a custom user model<a class=\"heading-anchor\" href=\"#writing-a-manager-for-a-custom-user-model\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>You should also define a custom manager for your user model. If your user model\ndefines <code class=\"docutils literal notranslate\">username</code>, <code class=\"docutils literal notranslate\">email</code>, <code class=\"docutils literal notranslate\">is_staff</code>, <code class=\"docutils literal notranslate\">is_active</code>, <code class=\"docutils literal notranslate\">is_superuser</code>,\n<code class=\"docutils literal notranslate\">last_login</code>, and <code class=\"docutils literal notranslate\">date_joined</code> fields the same as Django’s default user,\nyou can install Django’s <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.UserManager\" title=\"django.contrib.auth.models.UserManager\"><code class=\"xref py py-class docutils literal notranslate\">UserManager</code></a>;\nhowever, if your user model defines different fields, you’ll need to define a\ncustom manager that extends <a class=\"reference internal\" href=\"#django.contrib.auth.models.BaseUserManager\" title=\"django.contrib.auth.models.BaseUserManager\"><code class=\"xref py py-class docutils literal notranslate\">BaseUserManager</code></a>\nproviding two additional methods:</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.CustomUserManager\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">models.</span><span class=\"sig-name descname\">CustomUserManager</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.CustomUserManager\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.CustomUserManager.create_user\">\n<span class=\"sig-name descname\">create_user</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">username_field</em>, <em class=\"sig-param\">password<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>other_fields</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.CustomUserManager.create_user\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The prototype of <code class=\"docutils literal notranslate\">create_user()</code> should accept the username field,\nplus all required fields as arguments. For example, if your user model\nuses <code class=\"docutils literal notranslate\">email</code> as the username field, and has <code class=\"docutils literal notranslate\">date_of_birth</code> as a\nrequired field, then <code class=\"docutils literal notranslate\">create_user</code> should be defined as:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span> <span class=\"nf\">create_user</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> email<span class=\"p\">,</span> date_of_birth<span class=\"p\">,</span> password<span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n    <span class=\"c1\"># create user here</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.CustomUserManager.create_superuser\">\n<span class=\"sig-name descname\">create_superuser</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">username_field</em>, <em class=\"sig-param\">password<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>other_fields</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.CustomUserManager.create_superuser\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The prototype of <code class=\"docutils literal notranslate\">create_superuser()</code> should accept the username\nfield, plus all required fields as arguments. For example, if your user\nmodel uses <code class=\"docutils literal notranslate\">email</code> as the username field, and has <code class=\"docutils literal notranslate\">date_of_birth</code>\nas a required field, then <code class=\"docutils literal notranslate\">create_superuser</code> should be defined as:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span> <span class=\"nf\">create_superuser</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> email<span class=\"p\">,</span> date_of_birth<span class=\"p\">,</span> password<span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n    <span class=\"c1\"># create superuser here</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n</dd></dl>\n\n</dd></dl>\n\n<p>For a <a class=\"reference internal\" href=\"/en/3.2/ref/models/fields/#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> in <a class=\"reference internal\" href=\"#django.contrib.auth.models.CustomUser.USERNAME_FIELD\" title=\"django.contrib.auth.models.CustomUser.USERNAME_FIELD\"><code class=\"xref py py-attr docutils literal notranslate\">USERNAME_FIELD</code></a> or\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.CustomUser.REQUIRED_FIELDS\" title=\"django.contrib.auth.models.CustomUser.REQUIRED_FIELDS\"><code class=\"xref py py-attr docutils literal notranslate\">REQUIRED_FIELDS</code></a>, these methods receive the value of the\n<a class=\"reference internal\" href=\"/en/3.2/ref/models/fields/#django.db.models.ForeignKey.to_field\" title=\"django.db.models.ForeignKey.to_field\"><code class=\"xref py py-attr docutils literal notranslate\">to_field</code></a> (the <a class=\"reference internal\" href=\"/en/3.2/ref/models/fields/#django.db.models.Field.primary_key\" title=\"django.db.models.Field.primary_key\"><code class=\"xref py py-attr docutils literal notranslate\">primary_key</code></a>\nby default) of an existing instance.</p>\n<p><a class=\"reference internal\" href=\"#django.contrib.auth.models.BaseUserManager\" title=\"django.contrib.auth.models.BaseUserManager\"><code class=\"xref py py-class docutils literal notranslate\">BaseUserManager</code></a> provides the following\nutility methods:</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.BaseUserManager\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">models.</span><span class=\"sig-name descname\">BaseUserManager</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.BaseUserManager\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.BaseUserManager.normalize_email\">\n<em class=\"property\"><span class=\"k\">classmethod</span> </em><span class=\"sig-name descname\">normalize_email</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">email</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.BaseUserManager.normalize_email\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Normalizes email addresses by lowercasing the domain portion of the\nemail address.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.BaseUserManager.get_by_natural_key\">\n<span class=\"sig-name descname\">get_by_natural_key</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">username</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.BaseUserManager.get_by_natural_key\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Retrieves a user instance using the contents of the field\nnominated by <code class=\"docutils literal notranslate\">USERNAME_FIELD</code>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.BaseUserManager.make_random_password\">\n<span class=\"sig-name descname\">make_random_password</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">length<span class=\"o\">=</span><span class=\"default_value\">10</span></em>, <em class=\"sig-param\">allowed_chars<span class=\"o\">=</span><span class=\"default_value\">'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.BaseUserManager.make_random_password\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a random password with the given length and given string of\nallowed characters. Note that the default value of <code class=\"docutils literal notranslate\">allowed_chars</code>\ndoesn’t contain letters that can cause user confusion, including:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">i</code>, <code class=\"docutils literal notranslate\">l</code>, <code class=\"docutils literal notranslate\">I</code>, and <code class=\"docutils literal notranslate\">1</code> (lowercase letter i, lowercase\nletter L, uppercase letter i, and the number one)</p></li>\n<li><p><code class=\"docutils literal notranslate\">o</code>, <code class=\"docutils literal notranslate\">O</code>, and <code class=\"docutils literal notranslate\">0</code> (lowercase letter o, uppercase letter o,\nand zero)</p></li>\n</ul>\n</dd></dl>\n\n</dd></dl>\n\n</section>\n<section id=\"extending-django-s-default-user\">\n<h3>Extending Django’s default <code class=\"docutils literal notranslate\">User</code><a class=\"heading-anchor\" href=\"#extending-django-s-default-user\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you’re entirely happy with Django’s <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a>\nmodel, but you want to add some additional profile information, you could\nsubclass <a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractUser\" title=\"django.contrib.auth.models.AbstractUser\"><code class=\"xref py py-class docutils literal notranslate\">django.contrib.auth.models.AbstractUser</code></a> and add your custom\nprofile fields, although we’d recommend a separate model as described in the\n“Model design considerations” note of <a class=\"reference internal\" href=\"#specifying-custom-user-model\"><span class=\"std std-ref\">Specifying a custom user model</span></a>.\n<code class=\"docutils literal notranslate\">AbstractUser</code> provides the full implementation of the default\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a> as an <a class=\"reference internal\" href=\"/en/3.2/topics/db/models/#abstract-base-classes\"><span class=\"std std-ref\">abstract model</span></a>.</p>\n</section>\n<section id=\"custom-users-and-the-built-in-auth-forms\">\n<span id=\"id2\"></span><h3>Custom users and the built-in auth forms<a class=\"heading-anchor\" href=\"#custom-users-and-the-built-in-auth-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django’s built-in <a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#built-in-auth-forms\"><span class=\"std std-ref\">forms</span></a> and <a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#built-in-auth-views\"><span class=\"std std-ref\">views</span></a> make certain assumptions about the user model that they\nare working with.</p>\n<p>The following forms are compatible with any subclass of\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser\" title=\"django.contrib.auth.models.AbstractBaseUser\"><code class=\"xref py py-class docutils literal notranslate\">AbstractBaseUser</code></a>:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#django.contrib.auth.forms.AuthenticationForm\" title=\"django.contrib.auth.forms.AuthenticationForm\"><code class=\"xref py py-class docutils literal notranslate\">AuthenticationForm</code></a>: Uses the username\nfield specified by <a class=\"reference internal\" href=\"#django.contrib.auth.models.CustomUser.USERNAME_FIELD\" title=\"django.contrib.auth.models.CustomUser.USERNAME_FIELD\"><code class=\"xref py py-attr docutils literal notranslate\">USERNAME_FIELD</code></a>.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#django.contrib.auth.forms.SetPasswordForm\" title=\"django.contrib.auth.forms.SetPasswordForm\"><code class=\"xref py py-class docutils literal notranslate\">SetPasswordForm</code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#django.contrib.auth.forms.PasswordChangeForm\" title=\"django.contrib.auth.forms.PasswordChangeForm\"><code class=\"xref py py-class docutils literal notranslate\">PasswordChangeForm</code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#django.contrib.auth.forms.AdminPasswordChangeForm\" title=\"django.contrib.auth.forms.AdminPasswordChangeForm\"><code class=\"xref py py-class docutils literal notranslate\">AdminPasswordChangeForm</code></a></p></li>\n</ul>\n<p>The following forms make assumptions about the user model and can be used as-is\nif those assumptions are met:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#django.contrib.auth.forms.PasswordResetForm\" title=\"django.contrib.auth.forms.PasswordResetForm\"><code class=\"xref py py-class docutils literal notranslate\">PasswordResetForm</code></a>: Assumes that the user\nmodel has a field that stores the user’s email address with the name returned\nby <a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser.get_email_field_name\" title=\"django.contrib.auth.models.AbstractBaseUser.get_email_field_name\"><code class=\"xref py py-meth docutils literal notranslate\">get_email_field_name()</code></a> (<code class=\"docutils literal notranslate\">email</code> by\ndefault) that can be used to identify the user and a boolean field named\n<code class=\"docutils literal notranslate\">is_active</code> to prevent password resets for inactive users.</p></li>\n</ul>\n<p>Finally, the following forms are tied to\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a> and need to be rewritten or extended\nto work with a custom user model:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#django.contrib.auth.forms.UserCreationForm\" title=\"django.contrib.auth.forms.UserCreationForm\"><code class=\"xref py py-class docutils literal notranslate\">UserCreationForm</code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#django.contrib.auth.forms.UserChangeForm\" title=\"django.contrib.auth.forms.UserChangeForm\"><code class=\"xref py py-class docutils literal notranslate\">UserChangeForm</code></a></p></li>\n</ul>\n<p>If your custom user model is a subclass of <code class=\"docutils literal notranslate\">AbstractUser</code>, then you can\nextend these forms in this manner:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.forms</span> <span class=\"kn\">import</span> UserCreationForm\n<span class=\"kn\">from</span> <span class=\"nn\">myapp.models</span> <span class=\"kn\">import</span> CustomUser\n\n<span class=\"k\">class</span> <span class=\"nc\">CustomUserCreationForm</span><span class=\"p\">(</span>UserCreationForm<span class=\"p\">):</span>\n\n    <span class=\"k\">class</span> <span class=\"nc\">Meta</span><span class=\"p\">(</span>UserCreationForm<span class=\"o\">.</span>Meta<span class=\"p\">):</span>\n        model <span class=\"o\">=</span> CustomUser\n        fields <span class=\"o\">=</span> UserCreationForm<span class=\"o\">.</span>Meta<span class=\"o\">.</span>fields <span class=\"o\">+</span> <span class=\"p\">(</span><span class=\"s1\">&#39;custom_field&#39;</span><span class=\"p\">,)</span>\n</code></pre></div>\n</section>\n<section id=\"custom-users-and-django-contrib-admin\">\n<h3>Custom users and <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/admin/#module-django.contrib.admin\" title=\"django.contrib.admin: Django's admin site.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.admin</code></a><a class=\"heading-anchor\" href=\"#custom-users-and-django-contrib-admin\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you want your custom user model to also work with the admin, your user model\nmust define some additional attributes and methods. These methods allow the\nadmin to control access of the user to admin content:</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">models.</span><span class=\"sig-name descname\">CustomUser</span></dt>\n<dd></dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.is_staff\">\n<span class=\"sig-name descname\">is_staff</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.is_staff\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\">True</code> if the user is allowed to have access to the admin site.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.is_active\">\n<span class=\"sig-name descname\">is_active</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.is_active\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\">True</code> if the user account is currently active.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\">\n<span class=\"sig-name descname\">has_perm(perm, obj=None):</span></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\">True</code> if the user has the named permission. If <code class=\"docutils literal notranslate\">obj</code> is\nprovided, the permission needs to be checked against a specific object\ninstance.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\">\n<span class=\"sig-name descname\">has_module_perms(app_label):</span></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\">True</code> if the user has permission to access models in\nthe given app.</p>\n</dd></dl>\n\n<p>You will also need to register your custom user model with the admin. If\nyour custom user model extends <code class=\"docutils literal notranslate\">django.contrib.auth.models.AbstractUser</code>,\nyou can use Django’s existing <code class=\"docutils literal notranslate\">django.contrib.auth.admin.UserAdmin</code>\nclass. However, if your user model extends\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.AbstractBaseUser\" title=\"django.contrib.auth.models.AbstractBaseUser\"><code class=\"xref py py-class docutils literal notranslate\">AbstractBaseUser</code></a>, you’ll need to define\na custom <code class=\"docutils literal notranslate\">ModelAdmin</code> class. It may be possible to subclass the default\n<code class=\"docutils literal notranslate\">django.contrib.auth.admin.UserAdmin</code>; however, you’ll need to\noverride any of the definitions that refer to fields on\n<code class=\"docutils literal notranslate\">django.contrib.auth.models.AbstractUser</code> that aren’t on your\ncustom user class.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>If you are using a custom <code class=\"docutils literal notranslate\">ModelAdmin</code> which is a subclass of\n<code class=\"docutils literal notranslate\">django.contrib.auth.admin.UserAdmin</code>, then you need to add your custom\nfields to <code class=\"docutils literal notranslate\">fieldsets</code> (for fields to be used in editing users) and to\n<code class=\"docutils literal notranslate\">add_fieldsets</code> (for fields to be used when creating a user). For\nexample:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.admin</span> <span class=\"kn\">import</span> UserAdmin\n\n<span class=\"k\">class</span> <span class=\"nc\">CustomUserAdmin</span><span class=\"p\">(</span>UserAdmin<span class=\"p\">):</span>\n    <span class=\"o\">...</span>\n    fieldsets <span class=\"o\">=</span> UserAdmin<span class=\"o\">.</span>fieldsets <span class=\"o\">+</span> <span class=\"p\">(</span>\n        <span class=\"p\">(</span><span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;fields&#39;</span><span class=\"p\">:</span> <span class=\"p\">(</span><span class=\"s1\">&#39;custom_field&#39;</span><span class=\"p\">,)}),</span>\n    <span class=\"p\">)</span>\n    add_fieldsets <span class=\"o\">=</span> UserAdmin<span class=\"o\">.</span>add_fieldsets <span class=\"o\">+</span> <span class=\"p\">(</span>\n        <span class=\"p\">(</span><span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;fields&#39;</span><span class=\"p\">:</span> <span class=\"p\">(</span><span class=\"s1\">&#39;custom_field&#39;</span><span class=\"p\">,)}),</span>\n    <span class=\"p\">)</span>\n</code></pre></div>\n<p>See <a class=\"reference internal\" href=\"#custom-users-admin-full-example\"><span class=\"std std-ref\">a full example</span></a> for more\ndetails.</p>\n</aside>\n</section>\n<section id=\"custom-users-and-permissions\">\n<h3>Custom users and permissions<a class=\"heading-anchor\" href=\"#custom-users-and-permissions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>To make it easy to include Django’s permission framework into your own user\nclass, Django provides <a class=\"reference internal\" href=\"#django.contrib.auth.models.PermissionsMixin\" title=\"django.contrib.auth.models.PermissionsMixin\"><code class=\"xref py py-class docutils literal notranslate\">PermissionsMixin</code></a>.\nThis is an abstract model you can include in the class hierarchy for your user\nmodel, giving you all the methods and database fields necessary to support\nDjango’s permission model.</p>\n<p><a class=\"reference internal\" href=\"#django.contrib.auth.models.PermissionsMixin\" title=\"django.contrib.auth.models.PermissionsMixin\"><code class=\"xref py py-class docutils literal notranslate\">PermissionsMixin</code></a> provides the following\nmethods and attributes:</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.PermissionsMixin\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">models.</span><span class=\"sig-name descname\">PermissionsMixin</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.PermissionsMixin\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.PermissionsMixin.is_superuser\">\n<span class=\"sig-name descname\">is_superuser</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.PermissionsMixin.is_superuser\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Boolean. Designates that this user has all permissions without\nexplicitly assigning them.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.PermissionsMixin.get_user_permissions\">\n<span class=\"sig-name descname\">get_user_permissions</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">obj<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.PermissionsMixin.get_user_permissions\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a set of permission strings that the user has directly.</p>\n<p>If <code class=\"docutils literal notranslate\">obj</code> is passed in, only returns the user permissions for this\nspecific object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.PermissionsMixin.get_group_permissions\">\n<span class=\"sig-name descname\">get_group_permissions</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">obj<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.PermissionsMixin.get_group_permissions\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a set of permission strings that the user has, through their\ngroups.</p>\n<p>If <code class=\"docutils literal notranslate\">obj</code> is passed in, only returns the group permissions for\nthis specific object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.PermissionsMixin.get_all_permissions\">\n<span class=\"sig-name descname\">get_all_permissions</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">obj<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.PermissionsMixin.get_all_permissions\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a set of permission strings that the user has, both through\ngroup and user permissions.</p>\n<p>If <code class=\"docutils literal notranslate\">obj</code> is passed in, only returns the permissions for this\nspecific object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.PermissionsMixin.has_perm\">\n<span class=\"sig-name descname\">has_perm</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">perm</em>, <em class=\"sig-param\">obj<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.PermissionsMixin.has_perm\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\">True</code> if the user has the specified permission, where\n<code class=\"docutils literal notranslate\">perm</code> is in the format <code class=\"docutils literal notranslate\">&quot;&lt;app label&gt;.&lt;permission codename&gt;&quot;</code> (see\n<a class=\"reference internal\" href=\"/en/3.2/topics/auth/default/#topic-authorization\"><span class=\"std std-ref\">permissions</span></a>). If <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.is_active\" title=\"django.contrib.auth.models.User.is_active\"><code class=\"xref py py-attr docutils literal notranslate\">User.is_active</code></a>\nand <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.is_superuser\" title=\"django.contrib.auth.models.User.is_superuser\"><code class=\"xref py py-attr docutils literal notranslate\">is_superuser</code></a> are both <code class=\"docutils literal notranslate\">True</code>, this method always\nreturns <code class=\"docutils literal notranslate\">True</code>.</p>\n<p>If <code class=\"docutils literal notranslate\">obj</code> is passed in, this method won’t check for a permission for\nthe model, but for this specific object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.PermissionsMixin.has_perms\">\n<span class=\"sig-name descname\">has_perms</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">perm_list</em>, <em class=\"sig-param\">obj<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.PermissionsMixin.has_perms\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\">True</code> if the user has each of the specified permissions,\nwhere each perm is in the format\n<code class=\"docutils literal notranslate\">&quot;&lt;app label&gt;.&lt;permission codename&gt;&quot;</code>. If <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.is_active\" title=\"django.contrib.auth.models.User.is_active\"><code class=\"xref py py-attr docutils literal notranslate\">User.is_active</code></a> and\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.is_superuser\" title=\"django.contrib.auth.models.User.is_superuser\"><code class=\"xref py py-attr docutils literal notranslate\">is_superuser</code></a> are both <code class=\"docutils literal notranslate\">True</code>, this method always\nreturns <code class=\"docutils literal notranslate\">True</code>.</p>\n<p>If <code class=\"docutils literal notranslate\">obj</code> is passed in, this method won’t check for permissions for\nthe model, but for the specific object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.models.PermissionsMixin.has_module_perms\">\n<span class=\"sig-name descname\">has_module_perms</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">package_name</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.models.PermissionsMixin.has_module_perms\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\">True</code> if the user has any permissions in the given package\n(the Django app label). If <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.is_active\" title=\"django.contrib.auth.models.User.is_active\"><code class=\"xref py py-attr docutils literal notranslate\">User.is_active</code></a> and\n<a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.is_superuser\" title=\"django.contrib.auth.models.User.is_superuser\"><code class=\"xref py py-attr docutils literal notranslate\">is_superuser</code></a> are both <code class=\"docutils literal notranslate\">True</code>, this method always\nreturns <code class=\"docutils literal notranslate\">True</code>.</p>\n</dd></dl>\n\n</dd></dl>\n\n<aside class=\"admonition-permissionsmixin-and-modelbackend admonition\" role=\"note\">\n<p class=\"admonition-title\"><code class=\"docutils literal notranslate\">PermissionsMixin</code> and <code class=\"docutils literal notranslate\">ModelBackend</code></p>\n<p>If you don’t include the\n<a class=\"reference internal\" href=\"#django.contrib.auth.models.PermissionsMixin\" title=\"django.contrib.auth.models.PermissionsMixin\"><code class=\"xref py py-class docutils literal notranslate\">PermissionsMixin</code></a>, you must ensure you\ndon’t invoke the permissions methods on <code class=\"docutils literal notranslate\">ModelBackend</code>. <code class=\"docutils literal notranslate\">ModelBackend</code>\nassumes that certain fields are available on your user model. If your user\nmodel doesn’t provide  those fields, you’ll receive database errors when\nyou check permissions.</p>\n</aside>\n</section>\n<section id=\"custom-users-and-proxy-models\">\n<h3>Custom users and proxy models<a class=\"heading-anchor\" href=\"#custom-users-and-proxy-models\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>One limitation of custom user models is that installing a custom user model\nwill break any proxy model extending <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a>.\nProxy models must be based on a concrete base class; by defining a custom user\nmodel, you remove the ability of Django to reliably identify the base class.</p>\n<p>If your project uses proxy models, you must either modify the proxy to extend\nthe user model that’s in use in your project, or merge your proxy’s behavior\ninto your <a class=\"reference internal\" href=\"/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a> subclass.</p>\n</section>\n<section id=\"a-full-example\">\n<span id=\"custom-users-admin-full-example\"></span><h3>A full example<a class=\"heading-anchor\" href=\"#a-full-example\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Here is an example of an admin-compliant custom user app. This user model uses\nan email address as the username, and has a required date of birth; it\nprovides no permission checking beyond an <code class=\"docutils literal notranslate\">admin</code> flag on the user account.\nThis model would be compatible with all the built-in auth forms and views,\nexcept for the user creation forms. This example illustrates how most of the\ncomponents work together, but is not intended to be copied directly into\nprojects for production use.</p>\n<p>This code would all live in a <code class=\"docutils literal notranslate\">models.py</code> file for a custom\nauthentication app:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.models</span> <span class=\"kn\">import</span> <span class=\"p\">(</span>\n    BaseUserManager<span class=\"p\">,</span> AbstractBaseUser\n<span class=\"p\">)</span>\n\n\n<span class=\"k\">class</span> <span class=\"nc\">MyUserManager</span><span class=\"p\">(</span>BaseUserManager<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">create_user</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> email<span class=\"p\">,</span> date_of_birth<span class=\"p\">,</span> password<span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n        <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Creates and saves a User with the given email, date of</span>\n<span class=\"sd\">        birth and password.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"ow\">not</span> email<span class=\"p\">:</span>\n            <span class=\"k\">raise</span> <span class=\"ne\">ValueError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Users must have an email address&#39;</span><span class=\"p\">)</span>\n\n        user <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>model<span class=\"p\">(</span>\n            email<span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span>normalize_email<span class=\"p\">(</span>email<span class=\"p\">),</span>\n            date_of_birth<span class=\"o\">=</span>date_of_birth<span class=\"p\">,</span>\n        <span class=\"p\">)</span>\n\n        user<span class=\"o\">.</span>set_password<span class=\"p\">(</span>password<span class=\"p\">)</span>\n        user<span class=\"o\">.</span>save<span class=\"p\">(</span>using<span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span>_db<span class=\"p\">)</span>\n        <span class=\"k\">return</span> user\n\n    <span class=\"k\">def</span> <span class=\"nf\">create_superuser</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> email<span class=\"p\">,</span> date_of_birth<span class=\"p\">,</span> password<span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n        <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Creates and saves a superuser with the given email, date of</span>\n<span class=\"sd\">        birth and password.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        user <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>create_user<span class=\"p\">(</span>\n            email<span class=\"p\">,</span>\n            password<span class=\"o\">=</span>password<span class=\"p\">,</span>\n            date_of_birth<span class=\"o\">=</span>date_of_birth<span class=\"p\">,</span>\n        <span class=\"p\">)</span>\n        user<span class=\"o\">.</span>is_admin <span class=\"o\">=</span> <span class=\"kc\">True</span>\n        user<span class=\"o\">.</span>save<span class=\"p\">(</span>using<span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span>_db<span class=\"p\">)</span>\n        <span class=\"k\">return</span> user\n\n\n<span class=\"k\">class</span> <span class=\"nc\">MyUser</span><span class=\"p\">(</span>AbstractBaseUser<span class=\"p\">):</span>\n    email <span class=\"o\">=</span> models<span class=\"o\">.</span>EmailField<span class=\"p\">(</span>\n        verbose_name<span class=\"o\">=</span><span class=\"s1\">&#39;email address&#39;</span><span class=\"p\">,</span>\n        max_length<span class=\"o\">=</span><span class=\"mi\">255</span><span class=\"p\">,</span>\n        unique<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">,</span>\n    <span class=\"p\">)</span>\n    date_of_birth <span class=\"o\">=</span> models<span class=\"o\">.</span>DateField<span class=\"p\">()</span>\n    is_active <span class=\"o\">=</span> models<span class=\"o\">.</span>BooleanField<span class=\"p\">(</span>default<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n    is_admin <span class=\"o\">=</span> models<span class=\"o\">.</span>BooleanField<span class=\"p\">(</span>default<span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n\n    objects <span class=\"o\">=</span> MyUserManager<span class=\"p\">()</span>\n\n    USERNAME_FIELD <span class=\"o\">=</span> <span class=\"s1\">&#39;email&#39;</span>\n    REQUIRED_FIELDS <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;date_of_birth&#39;</span><span class=\"p\">]</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>email\n\n    <span class=\"k\">def</span> <span class=\"nf\">has_perm</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> perm<span class=\"p\">,</span> obj<span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n        <span class=\"s2\">&quot;Does the user have a specific permission?&quot;</span>\n        <span class=\"c1\"># Simplest possible answer: Yes, always</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">has_module_perms</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> app_label<span class=\"p\">):</span>\n        <span class=\"s2\">&quot;Does the user have permissions to view the app `app_label`?&quot;</span>\n        <span class=\"c1\"># Simplest possible answer: Yes, always</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"nd\">@property</span>\n    <span class=\"k\">def</span> <span class=\"nf\">is_staff</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"s2\">&quot;Is the user a member of staff?&quot;</span>\n        <span class=\"c1\"># Simplest possible answer: All admins are staff</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>is_admin\n</code></pre></div>\n<p>Then, to register this custom user model with Django’s admin, the following\ncode would be required in the app’s <code class=\"docutils literal notranslate\">admin.py</code> file:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django</span> <span class=\"kn\">import</span> forms\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib</span> <span class=\"kn\">import</span> admin\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.models</span> <span class=\"kn\">import</span> Group\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.admin</span> <span class=\"kn\">import</span> UserAdmin <span class=\"k\">as</span> BaseUserAdmin\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.forms</span> <span class=\"kn\">import</span> ReadOnlyPasswordHashField\n<span class=\"kn\">from</span> <span class=\"nn\">django.core.exceptions</span> <span class=\"kn\">import</span> ValidationError\n\n<span class=\"kn\">from</span> <span class=\"nn\">customauth.models</span> <span class=\"kn\">import</span> MyUser\n\n\n<span class=\"k\">class</span> <span class=\"nc\">UserCreationForm</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>ModelForm<span class=\"p\">):</span>\n    <span class=\"sd\">&quot;&quot;&quot;A form for creating new users. Includes all the required</span>\n<span class=\"sd\">    fields, plus a repeated password.&quot;&quot;&quot;</span>\n    password1 <span class=\"o\">=</span> forms<span class=\"o\">.</span>CharField<span class=\"p\">(</span>label<span class=\"o\">=</span><span class=\"s1\">&#39;Password&#39;</span><span class=\"p\">,</span> widget<span class=\"o\">=</span>forms<span class=\"o\">.</span>PasswordInput<span class=\"p\">)</span>\n    password2 <span class=\"o\">=</span> forms<span class=\"o\">.</span>CharField<span class=\"p\">(</span>label<span class=\"o\">=</span><span class=\"s1\">&#39;Password confirmation&#39;</span><span class=\"p\">,</span> widget<span class=\"o\">=</span>forms<span class=\"o\">.</span>PasswordInput<span class=\"p\">)</span>\n\n    <span class=\"k\">class</span> <span class=\"nc\">Meta</span><span class=\"p\">:</span>\n        model <span class=\"o\">=</span> MyUser\n        fields <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"s1\">&#39;email&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;date_of_birth&#39;</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">clean_password2</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># Check that the two password entries match</span>\n        password1 <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>cleaned_data<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;password1&quot;</span><span class=\"p\">)</span>\n        password2 <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>cleaned_data<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;password2&quot;</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> password1 <span class=\"ow\">and</span> password2 <span class=\"ow\">and</span> password1 <span class=\"o\">!=</span> password2<span class=\"p\">:</span>\n            <span class=\"k\">raise</span> ValidationError<span class=\"p\">(</span><span class=\"s2\">&quot;Passwords don&#39;t match&quot;</span><span class=\"p\">)</span>\n        <span class=\"k\">return</span> password2\n\n    <span class=\"k\">def</span> <span class=\"nf\">save</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> commit<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">):</span>\n        <span class=\"c1\"># Save the provided password in hashed format</span>\n        user <span class=\"o\">=</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>save<span class=\"p\">(</span>commit<span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n        user<span class=\"o\">.</span>set_password<span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span>cleaned_data<span class=\"p\">[</span><span class=\"s2\">&quot;password1&quot;</span><span class=\"p\">])</span>\n        <span class=\"k\">if</span> commit<span class=\"p\">:</span>\n            user<span class=\"o\">.</span>save<span class=\"p\">()</span>\n        <span class=\"k\">return</span> user\n\n\n<span class=\"k\">class</span> <span class=\"nc\">UserChangeForm</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>ModelForm<span class=\"p\">):</span>\n    <span class=\"sd\">&quot;&quot;&quot;A form for updating users. Includes all the fields on</span>\n<span class=\"sd\">    the user, but replaces the password field with admin&#39;s</span>\n<span class=\"sd\">    disabled password hash display field.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    password <span class=\"o\">=</span> ReadOnlyPasswordHashField<span class=\"p\">()</span>\n\n    <span class=\"k\">class</span> <span class=\"nc\">Meta</span><span class=\"p\">:</span>\n        model <span class=\"o\">=</span> MyUser\n        fields <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"s1\">&#39;email&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;password&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;date_of_birth&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;is_active&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;is_admin&#39;</span><span class=\"p\">)</span>\n\n\n<span class=\"k\">class</span> <span class=\"nc\">UserAdmin</span><span class=\"p\">(</span>BaseUserAdmin<span class=\"p\">):</span>\n    <span class=\"c1\"># The forms to add and change user instances</span>\n    form <span class=\"o\">=</span> UserChangeForm\n    add_form <span class=\"o\">=</span> UserCreationForm\n\n    <span class=\"c1\"># The fields to be used in displaying the User model.</span>\n    <span class=\"c1\"># These override the definitions on the base UserAdmin</span>\n    <span class=\"c1\"># that reference specific fields on auth.User.</span>\n    list_display <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"s1\">&#39;email&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;date_of_birth&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;is_admin&#39;</span><span class=\"p\">)</span>\n    list_filter <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"s1\">&#39;is_admin&#39;</span><span class=\"p\">,)</span>\n    fieldsets <span class=\"o\">=</span> <span class=\"p\">(</span>\n        <span class=\"p\">(</span><span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;fields&#39;</span><span class=\"p\">:</span> <span class=\"p\">(</span><span class=\"s1\">&#39;email&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;password&#39;</span><span class=\"p\">)}),</span>\n        <span class=\"p\">(</span><span class=\"s1\">&#39;Personal info&#39;</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;fields&#39;</span><span class=\"p\">:</span> <span class=\"p\">(</span><span class=\"s1\">&#39;date_of_birth&#39;</span><span class=\"p\">,)}),</span>\n        <span class=\"p\">(</span><span class=\"s1\">&#39;Permissions&#39;</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;fields&#39;</span><span class=\"p\">:</span> <span class=\"p\">(</span><span class=\"s1\">&#39;is_admin&#39;</span><span class=\"p\">,)}),</span>\n    <span class=\"p\">)</span>\n    <span class=\"c1\"># add_fieldsets is not a standard ModelAdmin attribute. UserAdmin</span>\n    <span class=\"c1\"># overrides get_fieldsets to use this attribute when creating a user.</span>\n    add_fieldsets <span class=\"o\">=</span> <span class=\"p\">(</span>\n        <span class=\"p\">(</span><span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"p\">{</span>\n            <span class=\"s1\">&#39;classes&#39;</span><span class=\"p\">:</span> <span class=\"p\">(</span><span class=\"s1\">&#39;wide&#39;</span><span class=\"p\">,),</span>\n            <span class=\"s1\">&#39;fields&#39;</span><span class=\"p\">:</span> <span class=\"p\">(</span><span class=\"s1\">&#39;email&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;date_of_birth&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;password1&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;password2&#39;</span><span class=\"p\">),</span>\n        <span class=\"p\">}),</span>\n    <span class=\"p\">)</span>\n    search_fields <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"s1\">&#39;email&#39;</span><span class=\"p\">,)</span>\n    ordering <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"s1\">&#39;email&#39;</span><span class=\"p\">,)</span>\n    filter_horizontal <span class=\"o\">=</span> <span class=\"p\">()</span>\n\n\n<span class=\"c1\"># Now register the new UserAdmin...</span>\nadmin<span class=\"o\">.</span>site<span class=\"o\">.</span>register<span class=\"p\">(</span>MyUser<span class=\"p\">,</span> UserAdmin<span class=\"p\">)</span>\n<span class=\"c1\"># ... and, since we&#39;re not using Django&#39;s built-in permissions,</span>\n<span class=\"c1\"># unregister the Group model from admin.</span>\nadmin<span class=\"o\">.</span>site<span class=\"o\">.</span>unregister<span class=\"p\">(</span>Group<span class=\"p\">)</span>\n</code></pre></div>\n<p>Finally, specify the custom model as the default user model for your project\nusing the <a class=\"reference internal\" href=\"/en/3.2/ref/settings/#std-setting-AUTH_USER_MODEL\"><code class=\"xref std std-setting docutils literal notranslate\">AUTH_USER_MODEL</code></a> setting in your <code class=\"docutils literal notranslate\">settings.py</code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>AUTH_USER_MODEL <span class=\"o\">=</span> <span class=\"s1\">&#39;customauth.MyUser&#39;</span>\n</code></pre></div>\n<aside class=\"version-note version-changed\" data-version=\"3.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 3.2</p><p>In older versions, <code class=\"docutils literal notranslate\">ReadOnlyPasswordHashField</code> is not\n<a class=\"reference internal\" href=\"/en/3.2/ref/forms/fields/#django.forms.Field.disabled\" title=\"django.forms.Field.disabled\"><code class=\"xref py py-attr docutils literal notranslate\">disabled</code></a> by default and\n<code class=\"docutils literal notranslate\">UserChangeForm.clean_password()</code> is required to return the initial\nvalue, whatever the user provides.</p>\n</aside>\n</section>\n</section>","rootId":"customizing-authentication-in-django","toc":[{"title":"Other authentication sources","anchor":"other-authentication-sources","children":[{"title":"Specifying authentication backends","anchor":"specifying-authentication-backends","children":[]},{"title":"Writing an authentication backend","anchor":"writing-an-authentication-backend","children":[]},{"title":"Handling authorization in custom backends","anchor":"handling-authorization-in-custom-backends","children":[{"title":"Authorization for anonymous users","anchor":"authorization-for-anonymous-users","children":[]},{"title":"Authorization for inactive users","anchor":"authorization-for-inactive-users","children":[]},{"title":"Handling object permissions","anchor":"handling-object-permissions","children":[]}]}]},{"title":"Custom permissions","anchor":"custom-permissions","children":[]},{"title":"Extending the existing User model","anchor":"extending-the-existing-user-model","children":[]},{"title":"Substituting a custom User model","anchor":"substituting-a-custom-user-model","children":[{"title":"Using a custom user model when starting a project","anchor":"using-a-custom-user-model-when-starting-a-project","children":[]},{"title":"Changing to a custom user model mid-project","anchor":"changing-to-a-custom-user-model-mid-project","children":[]},{"title":"Reusable apps and AUTH_USER_MODEL","anchor":"reusable-apps-and-auth-user-model","children":[]},{"title":"Referencing the User model","anchor":"referencing-the-user-model","children":[]},{"title":"Specifying a custom user model","anchor":"specifying-a-custom-user-model","children":[]},{"title":"Writing a manager for a custom user model","anchor":"writing-a-manager-for-a-custom-user-model","children":[]},{"title":"Extending Django’s default User","anchor":"extending-django-s-default-user","children":[]},{"title":"Custom users and the built-in auth forms","anchor":"custom-users-and-the-built-in-auth-forms","children":[]},{"title":"Custom users and django.contrib.admin","anchor":"custom-users-and-django-contrib-admin","children":[]},{"title":"Custom users and permissions","anchor":"custom-users-and-permissions","children":[]},{"title":"Custom users and proxy models","anchor":"custom-users-and-proxy-models","children":[]},{"title":"A full example","anchor":"a-full-example","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/en/3.2/topics/"},{"docname":"topics/auth/index","title":"User authentication in Django","url":"/en/3.2/topics/auth/"}],"prev":{"docname":"topics/auth/passwords","title":"Password management in Django","url":"/en/3.2/topics/auth/passwords/"},"next":{"docname":"topics/cache","title":"Django’s cache framework","url":"/en/3.2/topics/cache/"},"formats":{"html":"/en/3.2/topics/auth/customizing/","markdown":"/en/3.2/topics/auth/customizing.md","json":"/en/3.2/topics/auth/customizing.json"},"source":"https://github.com/django/django/blob/stable/3.2.x/docs/topics/auth/customizing.txt","official":"https://docs.djangoproject.com/en/3.2/topics/auth/customizing/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}