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
860
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
都市をデータで見るってこういうこと PLATEAU属性情報入門
nokonoko1203
1
530
Rails産でないDBを Railsに引っ越すHACK - Omotesando.rb #110
lnit
1
160
KotlinConf 2025 現地で感じたServer-Side Kotlin
n_takehata
1
220
Julia という言語について (FP in Julia « SIDE: F ») for 関数型まつり2025
antimon2
3
960
つよそうにふるまい、つよい成果を出すのなら、つよいのかもしれない
irof
1
290
データの民主化を支える、透明性のあるデータ利活用への挑戦 2025-06-25 Database Engineering Meetup#7
y_ken
0
220
生成AIコーディングとの向き合い方、AIと共創するという考え方 / How to deal with generative AI coding and the concept of co-creating with AI
seike460
PRO
1
310
エンジニア向け採用ピッチ資料
inusan
0
130
LINEヤフー データグループ紹介
lycorp_recruit_jp
0
750
FormFlow - Build Stunning Multistep Forms
yceruto
1
180
Benchmark
sysong
0
210
A comprehensive view of refactoring
marabesi
0
810
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
What's in a price? How to price your products and services
michaelherold
245
12k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
Git: the NoSQL Database
bkeepers
PRO
430
65k
Gamification - CAS2011
davidbonilla
81
5.3k
GraphQLとの向き合い方2022年版
quramy
46
14k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
6
700
Building a Modern Day E-commerce SEO Strategy
aleyda
41
7.3k
Code Review Best Practice
trishagee
68
18k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
53k
YesSQL, Process and Tooling at Scale
rocio
172
14k
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/