Upgrade to Pro — share decks privately, control downloads, hide ads and more …

django-authtools - A Custom User Model For Everyone

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

Other Decks in Programming

Transcript

  1. Why a Custom User? • Email as username. ◦ Most

    apps don’t really need a username
  2. Why a Custom User? • Email as username. ◦ Most

    apps don’t really need a username • Name ◦ First/Last name is wrong.
  3. 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
  4. 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()
  5. Installation 1. Add authtools to your INSTALLED_APPS 2. AUTH_USER_MODEL =

    ‘authtools.User’ 3. url(r'^accounts/', include('authtools.urls'))
  6. Extensible Views And (mostly) drop in class based replacements for

    all of the views provided by the built in auth app.
  7. 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.
  8. Generic Forms • built in UserChangeForm and UserCreationForm don’t work

    with custom users. • django-authtools forms do.
  9. django-authtools • email as username • name instead of first_name,

    last_name • extensible class based views • better password reset • generic forms • generic admin
  10. More on Migrations • Unique constraints (email address) • 3rd

    party applications aren’t all up to speed.
  11. Don’t do this • Not reusable • Mixes profile code

    with authentication and authorization.
  12. 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’
  13. 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`
  14. 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/