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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Aaron Merriam
August 27, 2013
Programming
2
890
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
AgentCoreとHuman in the Loop
har1101
5
240
開発者から情シスまで - 多様なユーザー層に届けるAPI提供戦略 / Postman API Night Okinawa 2026 Winter
tasshi
0
200
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
200
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
250
Claude Codeと2つの巻き戻し戦略 / Two Rewind Strategies with Claude Code
fruitriin
0
140
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
590
CSC307 Lecture 05
javiergs
PRO
0
500
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
180
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
190
OCaml 5でモダンな並列プログラミングを Enjoyしよう!
haochenx
0
140
要求定義・仕様記述・設計・検証の手引き - 理論から学ぶ明確で統一された成果物定義
orgachem
PRO
1
160
Amazon Bedrockを活用したRAGの品質管理パイプライン構築
tosuri13
5
760
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.3k
A Soul's Torment
seathinner
5
2.3k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
250
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
120
Amusing Abliteration
ianozsvald
0
100
Optimizing for Happiness
mojombo
379
71k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
67
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
7.9k
The Curious Case for Waylosing
cassininazir
0
240
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
0
440
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
130
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/