-
Notifications
You must be signed in to change notification settings - Fork 0
/
django-authentication-by-email-no-username.html
368 lines (309 loc) · 32.1 KB
/
django-authentication-by-email-no-username.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Thomas Loiret">
<meta name="description" content="How to use the email as the user identifier and no longer use the username ?">
<meta name="fediverse:creator" content="@[email protected]">
<meta name="google-site-verification" content="jZBc2U9MHiBnSlgpoFEed-V3PZ16pMNv0GSX4hOh4mg" />
<title>Django authentication by email (no username) - Thomas Loiret - Random thoughts</title>
<!-- Custom styles for this template -->
<!-- build:css stylesheets/main.css -->
<link rel="stylesheet" href="https://b0uh.github.io/theme/css/main.min.css">
<link rel="stylesheet" href="https://b0uh.github.io/theme/css/custom.min.css">
<link rel="stylesheet" href="https://b0uh.github.io/theme/css/pygments/autumn.min.css">
<!-- endbuild -->
<link href="https://b0uh.github.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Thomas Loiret - Random thoughts Atom Feed" />
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<!-- Google Fonts -->
<script type="text/javascript">
WebFontConfig = {
google: { families: [ 'Open+Sans:300,400,700:latin', 'Lato:700,900:latin' ] }
};
(function() {
var wf = document.createElement('script');
wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
'://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
</script>
</head>
<body>
<!-- header -->
<header class="header push-down-45">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#readable-navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<nav class="navbar navbar-default" role="navigation">
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="readable-navbar-collapse">
<ul class="navigation">
<li>
<a href="https://b0uh.github.io">Home</a>
</li>
<li>
<a href="https://b0uh.github.io/pages/about.html">About</a>
</li>
<li>
<a href="https://b0uh.github.io/pages/bookmarks.html">Bookmarks</a>
</li>
<li>
<a href="https://b0uh.github.io/archives.html">Archives</a>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</nav>
<div class="hidden-xs hidden-sm">
<div class="social">
<a href="#" class="search__container">
<span class="glyphicon glyphicon-flash"></span>
</a>
<ul class="social__dropdown">
<li>
<a href="https://b0uh.github.io/feeds/all.atom.xml" target="_blank" class="social__container">
<span class="zocial-rss"></span>
</a>
</li>
<li>
<a href="https://mamot.fr/@b0uh" rel="me" target="_blank" class="social__container">
<img src="https://b0uh.github.io/theme/images/icons/mastodon-25px.png" />
</a>
</li>
<li>
<a href="https://www.linkedin.com/in/thomasloiret/" target="_blank" class="social__container">
<span class="zocial-linkedin"></span>
</a>
</li>
<li>
<a href="https://www.last.fm/user/b0uh" target="_blank" class="social__container">
<span class="zocial-lastfm"></span>
</a>
</li>
</ul>
</div>
</div>
</div>
</header>
<div class="container">
<div itemscope itemtype="http://schema.org/Article" class="boxed push-down-60">
<!-- Start of the blogpost -->
<div class="row">
<div class="col-xs-10 col-xs-offset-1 col-md-8 col-md-offset-2 push-down-60">
<!-- Start of the content -->
<div class="post-content">
<h1 itemprop="name">Django authentication by email (no username)</h1>
<div class="meta__container--without-image">
<div class="row">
<div class="col-xs-12 col-sm-8">
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
<div class="meta__info" itemprop="name">
Thomas Loiret
</div>
</span>
</div>
<div class="col-xs-12 col-sm-4">
<div class="meta__comments">
<span class="meta__date" itemprop="datePublished" content="2017-06-14">
<span class="glyphicon glyphicon-calendar"></span> Wed 14 June 2017
</span>
</div>
</div>
</div>
</div>
<span itemprop="articleBody">
<h2>Goal</h2>
<ul>
<li>Allow user to register and log with email/password and not username/password.</li>
<li>Keep the behaviour as close as possible from Django defaults.<ul>
<li>manage.py createsuperuser with email</li>
<li>Admin creation and update</li>
</ul>
</li>
<li>Redefine the fewer code possible</li>
</ul>
<h2>Step 1: User model</h2>
<p>The first thing to do is to redefine the <code>User</code> model. Unlike Django documentation, our model will not inherit <a href="https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#django.contrib.auth.models.AbstractUser"><code>AbstractUser</code></a>, but <a href="https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#django.contrib.auth.models.AbstractBaseUser"><code>AbstractBaseUser</code></a> because we don't want the username field anymore. Except from that, all the code below is pretty much the same as <code>AbstractUser</code> as we can see it from Django's code. We also inherit from <code>PermissionsMixin</code> so we still get the permission and group feature available.</p>
<div class="highlight"><pre><span></span><code><span class="kn">from</span><span class="w"> </span><span class="nn">django.db</span><span class="w"> </span><span class="kn">import</span><span class="w"> </span><span class="n">models</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.contrib.auth.models</span><span class="w"> </span><span class="kn">import</span><span class="w"> </span><span class="n">AbstractBaseUser</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.contrib.auth.models</span><span class="w"> </span><span class="kn">import</span><span class="w"> </span><span class="n">PermissionsMixin</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.core.mail</span><span class="w"> </span><span class="kn">import</span><span class="w"> </span><span class="n">send_mail</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils</span><span class="w"> </span><span class="kn">import</span><span class="w"> </span><span class="n">timezone</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.translation</span><span class="w"> </span><span class="kn">import</span><span class="w"> </span><span class="n">ugettext_lazy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">_</span>
<span class="k">class</span><span class="w"> </span><span class="nc">User</span><span class="p">(</span><span class="n">AbstractBaseUser</span><span class="p">,</span><span class="w"> </span><span class="n">PermissionsMixin</span><span class="p">):</span>
<span class="w"> </span><span class="n">email</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">models</span><span class="o">.</span><span class="n">EmailField</span><span class="p">(</span><span class="n">unique</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="w"> </span><span class="n">is_staff</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">models</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">(</span>
<span class="w"> </span><span class="n">_</span><span class="p">(</span><span class="s1">'staff status'</span><span class="p">),</span>
<span class="w"> </span><span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="w"> </span><span class="n">help_text</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s1">'Designates whether the user can log into this site.'</span><span class="p">),</span>
<span class="w"> </span><span class="p">)</span>
<span class="w"> </span><span class="n">is_active</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">models</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">(</span>
<span class="w"> </span><span class="n">_</span><span class="p">(</span><span class="s1">'active'</span><span class="p">),</span>
<span class="w"> </span><span class="n">default</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="w"> </span><span class="n">help_text</span><span class="o">=</span><span class="n">_</span><span class="p">(</span>
<span class="w"> </span><span class="s1">'Designates whether this user should be treated as active. '</span>
<span class="w"> </span><span class="s1">'Unselect this instead of deleting accounts.'</span>
<span class="w"> </span><span class="p">),</span>
<span class="w"> </span><span class="p">)</span>
<span class="w"> </span><span class="n">first_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">'first name'</span><span class="p">),</span><span class="w"> </span><span class="n">max_length</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span><span class="w"> </span><span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="w"> </span><span class="n">last_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">'last name'</span><span class="p">),</span><span class="w"> </span><span class="n">max_length</span><span class="o">=</span><span class="mi">150</span><span class="p">,</span><span class="w"> </span><span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="w"> </span><span class="n">date_joined</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">'date joined'</span><span class="p">),</span><span class="w"> </span><span class="n">default</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">)</span>
<span class="w"> </span><span class="n">objects</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">UserManager</span><span class="p">()</span>
<span class="w"> </span><span class="n">USERNAME_FIELD</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'email'</span>
<span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nc">Meta</span><span class="p">:</span>
<span class="w"> </span><span class="n">verbose_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_</span><span class="p">(</span><span class="s1">'user'</span><span class="p">)</span>
<span class="w"> </span><span class="n">verbose_name_plural</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_</span><span class="p">(</span><span class="s1">'users'</span><span class="p">)</span>
<span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w"> </span><span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">clean</span><span class="p">()</span>
<span class="w"> </span><span class="bp">self</span><span class="o">.</span><span class="n">email</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">normalize_email</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">email</span><span class="p">)</span>
<span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">get_short_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w"> </span><span class="s2">"""Return the short name for the user."""</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="bp">self</span><span class="o">.</span><span class="n">first_name</span>
<span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">email_user</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">subject</span><span class="p">,</span><span class="w"> </span><span class="n">message</span><span class="p">,</span><span class="w"> </span><span class="n">from_email</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="w"> </span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w"> </span><span class="s2">"""Send an email to this user."""</span>
<span class="w"> </span><span class="n">send_mail</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span><span class="w"> </span><span class="n">message</span><span class="p">,</span><span class="w"> </span><span class="n">from_email</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">email</span><span class="p">],</span><span class="w"> </span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</code></pre></div>
<p>Once it is done, add to your setting <code>AUTH_USER_MODEL</code>:</p>
<div class="highlight"><pre><span></span><code>AUTH_USER_MODEL = 'my_app_user.User'
</code></pre></div>
<h2>Step 2: User manager</h2>
<p>With just the model things won't work, you also need to redefine the Manager so it doesn't take into account the username anymore. Again this looks a lot like the default <a href="https://docs.djangoproject.com/en/1.11/ref/contrib/auth/#manager-methods"><code>UserManager</code></a>'s code.</p>
<div class="highlight"><pre><span></span><code><span class="kn">from</span><span class="w"> </span><span class="nn">django.contrib.auth.models</span><span class="w"> </span><span class="kn">import</span><span class="w"> </span><span class="n">BaseUserManager</span>
<span class="k">class</span><span class="w"> </span><span class="nc">UserManager</span><span class="p">(</span><span class="n">BaseUserManager</span><span class="p">):</span>
<span class="w"> </span><span class="s2">"""Redefine user manager with email as identifier."""</span>
<span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">_create_user</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">email</span><span class="p">,</span><span class="w"> </span><span class="n">password</span><span class="p">,</span><span class="w"> </span><span class="o">**</span><span class="n">extra_fields</span><span class="p">):</span>
<span class="w"> </span><span class="s2">"""</span>
<span class="s2"> Creates and saves a User with the given email and password.</span>
<span class="s2"> """</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="ow">not</span><span class="w"> </span><span class="n">email</span><span class="p">:</span>
<span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="n">ValueError</span><span class="p">(</span><span class="s1">'The Email must be set'</span><span class="p">)</span>
<span class="w"> </span><span class="n">email</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="o">.</span><span class="n">normalize_email</span><span class="p">(</span><span class="n">email</span><span class="p">)</span>
<span class="w"> </span><span class="n">user</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span><span class="n">email</span><span class="o">=</span><span class="n">email</span><span class="p">,</span><span class="w"> </span><span class="o">**</span><span class="n">extra_fields</span><span class="p">)</span>
<span class="w"> </span><span class="n">user</span><span class="o">.</span><span class="n">set_password</span><span class="p">(</span><span class="n">password</span><span class="p">)</span>
<span class="w"> </span><span class="n">user</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">user</span>
<span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">create_user</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">email</span><span class="p">,</span><span class="w"> </span><span class="n">password</span><span class="p">,</span><span class="w"> </span><span class="o">**</span><span class="n">extra_fields</span><span class="p">):</span>
<span class="w"> </span><span class="n">extra_fields</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">'is_staff'</span><span class="p">,</span><span class="w"> </span><span class="kc">False</span><span class="p">)</span>
<span class="w"> </span><span class="n">extra_fields</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">'is_superuser'</span><span class="p">,</span><span class="w"> </span><span class="kc">False</span><span class="p">)</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="bp">self</span><span class="o">.</span><span class="n">_create_user</span><span class="p">(</span><span class="n">email</span><span class="p">,</span><span class="w"> </span><span class="n">password</span><span class="p">,</span><span class="w"> </span><span class="o">**</span><span class="n">extra_fields</span><span class="p">)</span>
<span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">create_superuser</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">email</span><span class="p">,</span><span class="w"> </span><span class="n">password</span><span class="p">,</span><span class="w"> </span><span class="o">**</span><span class="n">extra_fields</span><span class="p">):</span>
<span class="w"> </span><span class="n">extra_fields</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">'is_staff'</span><span class="p">,</span><span class="w"> </span><span class="kc">True</span><span class="p">)</span>
<span class="w"> </span><span class="n">extra_fields</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">'is_superuser'</span><span class="p">,</span><span class="w"> </span><span class="kc">True</span><span class="p">)</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="bp">self</span><span class="o">.</span><span class="n">_create_user</span><span class="p">(</span><span class="n">email</span><span class="p">,</span><span class="w"> </span><span class="n">password</span><span class="p">,</span><span class="w"> </span><span class="o">**</span><span class="n">extra_fields</span><span class="p">)</span>
</code></pre></div>
<p>So now we can create superuser with :</p>
<div class="highlight"><pre><span></span><code>python manage.py createsuperuser
</code></pre></div>
<h2>Step 3: Admininistration</h2>
<p>Like for the previous step, here I was inspired by default <a href="https://github.com/django/django/blob/master/django/contrib/auth/admin.py"><code>UserAdmin</code></a>. I just removed all trace of <code>username</code>.</p>
<div class="highlight"><pre><span></span><code><span class="kn">from</span><span class="w"> </span><span class="nn">django.contrib</span><span class="w"> </span><span class="kn">import</span><span class="w"> </span><span class="n">admin</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.contrib.auth.admin</span><span class="w"> </span><span class="kn">import</span><span class="w"> </span><span class="n">UserAdmin</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">DjangoUserAdmin</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.translation</span><span class="w"> </span><span class="kn">import</span><span class="w"> </span><span class="n">gettext_lazy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">_</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">user.models</span><span class="w"> </span><span class="kn">import</span><span class="w"> </span><span class="n">User</span>
<span class="k">class</span><span class="w"> </span><span class="nc">UserAdmin</span><span class="p">(</span><span class="n">DjangoUserAdmin</span><span class="p">):</span>
<span class="w"> </span><span class="n">fieldsets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span>
<span class="w"> </span><span class="p">(</span><span class="kc">None</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="s1">'fields'</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="s1">'email'</span><span class="p">,</span><span class="w"> </span><span class="s1">'password'</span><span class="p">)}),</span>
<span class="w"> </span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">'Personal info'</span><span class="p">),</span><span class="w"> </span><span class="p">{</span><span class="s1">'fields'</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="s1">'first_name'</span><span class="p">,</span><span class="w"> </span><span class="s1">'last_name'</span><span class="p">,)}),</span>
<span class="w"> </span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">'Permissions'</span><span class="p">),</span><span class="w"> </span><span class="p">{</span><span class="s1">'fields'</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="s1">'is_active'</span><span class="p">,</span><span class="w"> </span><span class="s1">'is_staff'</span><span class="p">,</span><span class="w"> </span><span class="s1">'is_superuser'</span><span class="p">,</span>
<span class="w"> </span><span class="s1">'groups'</span><span class="p">,</span><span class="w"> </span><span class="s1">'user_permissions'</span><span class="p">)}),</span>
<span class="w"> </span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">'Important dates'</span><span class="p">),</span><span class="w"> </span><span class="p">{</span><span class="s1">'fields'</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="s1">'last_login'</span><span class="p">,</span><span class="w"> </span><span class="s1">'date_joined'</span><span class="p">)}),</span>
<span class="w"> </span><span class="p">)</span>
<span class="w"> </span><span class="n">add_fieldsets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span>
<span class="w"> </span><span class="p">(</span><span class="kc">None</span><span class="p">,</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="s1">'classes'</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="s1">'wide'</span><span class="p">,),</span>
<span class="w"> </span><span class="s1">'fields'</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="s1">'email'</span><span class="p">,</span><span class="w"> </span><span class="s1">'password1'</span><span class="p">,</span><span class="w"> </span><span class="s1">'password2'</span><span class="p">),</span>
<span class="w"> </span><span class="p">}),</span>
<span class="w"> </span><span class="p">)</span>
<span class="w"> </span><span class="n">list_display</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'email'</span><span class="p">,</span><span class="w"> </span><span class="s1">'first_name'</span><span class="p">,</span><span class="w"> </span><span class="s1">'last_name'</span><span class="p">,</span><span class="w"> </span><span class="s1">'is_staff'</span><span class="p">)</span>
<span class="w"> </span><span class="n">list_filter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'is_staff'</span><span class="p">,</span><span class="w"> </span><span class="s1">'is_superuser'</span><span class="p">,</span><span class="w"> </span><span class="s1">'is_active'</span><span class="p">,</span><span class="w"> </span><span class="s1">'groups'</span><span class="p">)</span>
<span class="w"> </span><span class="n">search_fields</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'first_name'</span><span class="p">,</span><span class="w"> </span><span class="s1">'last_name'</span><span class="p">,</span><span class="w"> </span><span class="s1">'email'</span><span class="p">)</span>
<span class="w"> </span><span class="n">ordering</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'email'</span><span class="p">,)</span>
<span class="w"> </span><span class="n">filter_horizontal</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'groups'</span><span class="p">,</span><span class="w"> </span><span class="s1">'user_permissions'</span><span class="p">,)</span>
<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">User</span><span class="p">,</span><span class="w"> </span><span class="n">UserAdmin</span><span class="p">)</span>
</code></pre></div>
<h2>Conclusion</h2>
<p>We got an authentification closed to Django's defaults but with email and no longer with username.</p>
<p>The only thing missing is to regroup in the administration the <code>User</code> and <code>Group</code>. I didn't find any simple way to do it.</p>
<h3>Inspirations</h3>
<ul>
<li><a href="https://github.com/django/django/blob/master/django/contrib/auth/models.py">https://github.com/django/django/blob/master/django/contrib/auth/models.py</a></li>
<li><a href="https://github.com/django/django/blob/master/django/contrib/auth/base_user.py">https://github.com/django/django/blob/master/django/contrib/auth/base_user.py</a></li>
<li><a href="https://medium.com/@ramykhuffash/django-authentication-with-just-an-email-and-password-no-username-required-33e47976b517">https://medium.com/@ramykhuffash/django-authentication-with-just-an-email-and-password-no-username-required-33e47976b517</a></li>
<li><a href="https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#substituting-a-custom-user-model">https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#substituting-a-custom-user-model</a></li>
</ul>
</span>
<div class="meta__container--without-image">
<div class="row">
</div>
</div>
<br>
</div>
</div>
</div>
</div>
</div>
<footer class="copyrights">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-6">
Powered by <a href="https://github.com/getpelican/pelican">Pelican</a>.
</div>
<div class="col-xs-12 col-sm-6">
<div class="copyrights--right">
Made with <span class="glyphicon glyphicon-heart"></span> in Nantes.
</div>
</div>
</div>
</div>
</footer>
<script src="https://b0uh.github.io/theme/scripts/jquery.min.js"></script>
<script src="https://b0uh.github.io/theme/scripts/underscore.min.js"></script>
<script src="https://b0uh.github.io/theme/scripts/bootstrap.min.js"></script>
<script src="https://b0uh.github.io/theme/scripts/main.min.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-98369697-1', 'auto');
ga('send', 'pageview');
</script>
<!-- Chiffre.io - Privacy-first analytics -->
<script id="chiffre:analytics-config" type="application/json">
{
"publicKey": "pk.VvetZOQ0g5XwOL2W6hVvG7gFQTk3xdUMczDkqSCZUxE",
"pushURL": "https://push.chiffre.io/event/ceJKfYgcLN6XkYn8"
}
</script>
<script
src="https://embed.chiffre.io/analytics.js"
crossorigin="anonymous"
async=""
>
</script>
<noscript>
<img
src="https://push.chiffre.io/noscript/ceJKfYgcLN6XkYn8"
alt="Chiffre.io anonymous visit counting for clients without JavaScript"
crossorigin="anonymous"
width="1px"
height="1px"
style="display:none"
/>
</noscript>
</body>
</html>