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 Everyone
Search
Aaron Merriam
August 27, 2013
Programming
2
760
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
Hanami and htmx
bkuhlmann
0
200
スクラムガイドのスプリントレトロスペクティブを改めて読みかえしてみた / Re-reading the Sprint Retrospective Section in the Scrum Guide
mackey0225
3
400
educure_カリキュラム生操作マニュアル.pdf
linew_official
0
660
#phpcon_odawara オープン・クローズドなテストフィクスチャを求めて / open closed test fixtures
77web
3
220
Git Lint
bkuhlmann
4
750
Semantic search with Django and pgvector
pauloxnet
0
240
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
360
[技育CAMPアカデミア]アイディアを形に!【超入門】スマホアプリ開発〜リリースまでの流れをご紹介
teamlab
PRO
0
360
try! Swift Tokyo 2024のLT枠に採択されたプロポーザルを出すときに考えていたこと
ski
0
350
入門 AWS Amplify Gen2 / Introduction to AWS Amplify Gen2
genkiogasawara
1
320
SwiftUI Performance 不要なViewの再描画と更新を抑える
bigamitiongit
1
160
検証も兼ねて個人開発でHonoとかと向き合った話
hanetsuki
0
430
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
242
12k
The Invisible Side of Design
smashingmag
294
49k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
60
14k
Principles of Awesome APIs and How to Build Them.
keavy
120
16k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
13
1.5k
Build your cross-platform service in a week with App Engine
jlugia
225
17k
Web Components: a chance to create the future
zenorocha
305
41k
How to train your dragon (web standard)
notwaldorf
72
5.1k
Adopting Sorbet at Scale
ufuk
67
8.6k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Teambox: Starting and Learning
jrom
128
8.4k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
119
39k
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/