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
830
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
SwiftUI Viewの責務分離
elmetal
PRO
1
220
Formの複雑さに立ち向かう
bmthd
1
810
Amazon Q Developer Proで効率化するAPI開発入門
seike460
PRO
0
110
Domain-Driven Transformation
hschwentner
2
1.9k
AWS Organizations で実現する、 マルチ AWS アカウントのルートユーザー管理からの脱却
atpons
0
130
Honoとフロントエンドの 型安全性について
yodaka
5
330
CI改善もDatadogとともに
taumu
0
110
AWSマネコンに複数のアカウントで入れるようになりました
yuhta28
2
160
Writing documentation can be fun with plugin system
okuramasafumi
0
120
なぜイベント駆動が必要なのか - CQRS/ESで解く複雑系システムの課題 -
j5ik2o
9
3.4k
Flutter × Firebase Genkit で加速する生成 AI アプリ開発
coborinai
0
150
動作確認やテストで漏れがちな観点3選
starfish719
6
1k
Featured
See All Featured
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
174
51k
Music & Morning Musume
bryan
46
6.3k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
550
Designing for Performance
lara
604
68k
A designer walks into a library…
pauljervisheath
205
24k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.1k
Docker and Python
trallard
44
3.3k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
How to Ace a Technical Interview
jacobian
276
23k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Navigating Team Friction
lara
183
15k
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/