Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
django-authtools - A Custom User Model For Ever...
Search
Aaron Merriam
August 27, 2013
Programming
2
870
django-authtools - A Custom User Model For Everyone
Some basics about using a custom User model in Django, and how django-authtools makes it easier.
Aaron Merriam
August 27, 2013
Tweet
Share
Other Decks in Programming
See All in Programming
Reactをクライアントで使わない
yusukebe
7
5.4k
OWASP Kansai DAY 2025.09: OSINTにふれてみよう
deka_morita
0
130
AI Agents: How Do They Work and How to Build Them @ Shift 2025
slobodan
0
120
Introducing FrankenPHP gRPC
dunglas
2
820
デザイナーが Androidエンジニアに 挑戦してみた
874wokiite
0
660
2025年版 サーバーレス Web アプリケーションの作り方
hayatow
20
23k
Server Less Code More - コードを書かない時代に生きるサーバーレスデザイン / server-less-code-more
gawa
5
1.5k
Android StudioのAIコーディングツール、 ぶっちゃけどうなん???
mkeeda
0
180
Deep Dive into Kotlin Flow
jmatsu
1
420
さようなら Date。 ようこそTemporal! 3年間先行利用して得られた知見の共有
8beeeaaat
3
1.5k
はじめてのMaterial3 Expressive
ym223
2
1.1k
実践AIチャットボットUI実装入門
syumai
5
1.9k
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
860
Practical Orchestrator
shlominoach
190
11k
The Cult of Friendly URLs
andyhume
79
6.6k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Gamification - CAS2011
davidbonilla
81
5.4k
The Power of CSS Pseudo Elements
geoffreycrofte
78
6k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
A designer walks into a library…
pauljervisheath
208
24k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Building Adaptive Systems
keathley
43
2.8k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
Transcript
Django Custom User Models Aaron Merriam Rocky Meza
None
On Github • github.com/fusionbox • github.com/aaronmerriam • github.com/rockymeza
• Support introduced in 1.5. https://docs.djangoproject.com/en/dev/topics/auth/customizing/ Custom User Models
Why a Custom User? • Email as username. ◦ Most
apps don’t really need a username
Why a Custom User? • Email as username. ◦ Most
apps don’t really need a username • Name ◦ First/Last name is wrong.
Why a Custom User? • Email as username. ◦ Most
apps don’t really need a username • Name ◦ First/Last name is wrong. • Control over your User model ◦ methods and properties ◦ managers and queryset methods
How to use a Custom User Model?
Do this: models.py settings.py
Implement these: • Model ◦ USERNAME_FIELD ◦ REQUIRED_FIELDS ◦ is_active
◦ get_full_name() ◦ get_short_name() ◦ get_username() ◦ is_anonymous() ◦ is_authenticated() ◦ set_password() ◦ check_password() ◦ set_unusable_password() ◦ has_usable_password() • Custom Manager ◦ create_user() ◦ create_superuser()
Admin • UserCreationForm • UserChangeForm • ModelAdmin
Forms • User Creation • Update Account
models.py admin.py Full example (from Django docs)
Custom Login Logic? Custom Password Reset Logic?
A Custom User Model app for everyone! Introducing django-authtools
Installation 1. Add authtools to your INSTALLED_APPS 2. AUTH_USER_MODEL =
‘authtools.User’ 3. url(r'^accounts/', include('authtools.urls'))
Email As Username
Extensible Views
Extensible Views And (mostly) drop in class based replacements for
all of the views provided by the built in auth app.
Password Reset • built in password_reset_confirm doesn’t log you in?
• django-authtools provides an additional view password_reset_confirm_and_login that does.
Generic Forms • built in UserChangeForm and UserCreationForm don’t work
with custom users. • django-authtools forms do.
Generic ModelAdmin Classes
django-authtools • email as username • name instead of first_name,
last_name • extensible class based views • better password reset • generic forms • generic admin
Migrating to a Custom User (with South)
Choices • Ease of migration and model.Meta warts vs •
More difficult migration
More on Migrations • Unique constraints (email address) • 3rd
party applications aren’t all up to speed.
Storing additional user information.
Don’t do this
Don’t do this • Not reusable • Mixes profile code
with authentication and authorization.
UserProfiles
UserProfile support in Django settings.py models.py
Lets you do this.
Deprecated in 1.5
Still a great pattern.
Do this instead Don’t do this
And if you want to get fancy.
UserProfile
UserProfile • Reusable • Keeps authentication and authorization separate. •
Composability
Dos and Don’ts Don’t: Import `User` from `django.contrib.auth.models` or directly
reference that class anywhere in your code. Do: Use the ‘get_user_model’ function from ‘django. contrib.auth’
Dos and Don’ts Don’t: Make ForeignKeys that point to users
via the actual User model, or ‘auth.User’. Do: Point ForeignKeys to `settings.AUTH_USER_MODEL`
Links • django-authtools source ◦ https://github.com/fusionbox/django-authtools • django-authtools documentation ◦
https://django-authtools.readthedocs.org • Django documentation on custom user model ◦ https://docs.djangoproject. com/en/dev/topics/auth/customizing/ https://docs. djangoproject. com/en/1. 6/topics/auth/c ustomizing/