SlideShare a Scribd company logo
Two Scoops of Django
Chapter 16 - Dealing with the User Model

Alfred
When you check out
your user model…
or…
How it authenticates…
login

Authentication
Backends

authenticate

Model.User
Model.UserManager

pass

User
View

you know, the best way is to trace the python	

source code.	

backends: contrib.auth.backends.py	

model: contrib.auth.models.py	

admin(user view): contrib.auth.admin.py

•
•
•
Basic
From Django 1.5 onwards, the official preferred way to attach ForeignKey, OneToOneField, 	

or ManyToManyField to User is as follows:	


from django.conf import settings	
from django.db import models	

!
class IceCreamStore(models.model):	
owner = models.OneToOneField(settings.AUTH_USER_MODEL)	
title = models.CharField(max_length=255)
To customize your user
table, there are 3
options...
Warranty
We suggest that you carefully try out option #1
below, as it should work with a minimum of effort.
For Django 1.5-style custom User model definitions,
we recommend option #2 and option #3 for new
projects only.	

!

This is is because custom User model definitions for
option #2 and option #3 adds new User tables to
the database that will not have the existing project
data. Unless project-specific steps are taken to
address matters, migration means ORM connections
to related objects will be lost.
Option 1 - Link back
You continue to use User (called preferably via
django.contrib.auth.get user model()) and keep your related
fields in a separate model (e.g. Profile).	

from django.conf import settings	
from django.db import models	
class UserProfile(models.Model):	
# If you do this you need to either have a post_save signal or	
#

redirect to a profile_edit view on initial login.	

user = models.OneToOneField(settings.AUTH_USER_MODEL)	
favorite_ice_cream = models.CharField(max_length=30)
Option 2 AbstractUser
Choose this option if you like Django’s User model fields the
way they are, but need extra fields	

!from

django.contrib.auth.models import AbstractUser	

from django.db import models	
from django.utils.translation import ugettext_lazy as _	

!
class KarmaUser(AbstractUser):	
karma = models.PositiveIntegerField(_("karma"),	
default=0,	
blank=True)	
#Setting.py	
AUTH_USER_MODEL = "profile.KarmaUser"
Option 3 - Subclass
AbstractBaseUser
	


AbstractBaseUser is the bare-bones option with only 3 fields:
password, last login, and is active.	

!

	

 Let’s try it out with a custom User model for the Two
Scoops project. Here are our requirements: 	

We need an email address. 	

We need to handle permissions per the traditional
django.contrib.auth.models use of PermissionsMixin;
providing standard behavior for the Django admin. 	

We don’t need the first or last name of a user. 	

We need to know their favorite ice cream topping. 	


•
•
•
•
Option 3 - cont.(1)

	


1. Start from model.	

2. Model.UserManager create User -> override it to create
TwoScoopsUser	

3. We need TwoScoopsUser!
class TwoScoopsUser(AbstractBaseUser, PermissionsMixin):	
email = models.EmailField(	
verbose_name='email address',	
max_length=255,	
unique=True,	
db_index=True,	
)	
favorite_topping = models.CharField(max_length=255)	
is_active = models.BooleanField(default=True)	
is_admin = models.BooleanField(default=False)	
is_staff = models.BooleanField(default=False)	

!
!

objects = TwoScoopsUserManager()	
USERNAME_FIELD = 'email'	
REQUIRED_FIELDS = ['favorite_topping']
Option 3 - cont.(2)
Implement usage functions…
def get_full_name(self): return self.email	

!
def get_short_name(self): return self.email	

!
def __unicode__(self): return self.email	

!
def has_perm(self, perm, obj=None): return True	

!
def has_module_perms(self, app_label): return True
Option 3 - cont. (3)

	


Let’s write Model.UserManager to create TwoScoopsUser
class TwoScoopsUserManager(BaseUserManager):	
def create_user(self, email, favorite_topping, password=None):	
"""	
Creates and saves a User with the given email, date of	
birth and password.	
"""	
##Skip…	
user = self.model(	
email=TwoScoopsUserManager.normalize_email(email),	
favorite_topping=favorite_topping,	
)	

!
user.set_password(password)	
user.save(using=self._db)	
return user
Option 3 - cont. (4)

	


Let’s write Model.UserManager to create TwoScoopsUser
!
def create_superuser(self, email, favorite_topping, password):	
"""	
Creates and saves a superuser with the given email, date of	
birth and password.	
"""	
user = self.create_user(email,	
password=password,	
favorite_topping=favorite_topping	
)	
user.is_admin = True	
user.is_staff = True	
user.is_superuser = True	
user.save(using=self._db)	
return user
Option 3 - Let Admin
show your data.
	


Basically, override contrib.auth.admin …
class TwoScoopsUserAdmin(UserAdmin):	
# The forms to add and change user instances	
form = TwoScoopsUserChangeForm	
add_form = TwoScoopsUserCreationForm	

!

!

# The fields to be used in displaying the User model.	
# These override the definitions on the base UserAdmin	
# that reference specific fields on auth.User.	
list_display = ("email", "is_staff", "favorite_topping")	
list_filter = ("is_staff", "is_superuser", "is_active", "groups")	
search_fields = ("email", "favorite_topping")	
ordering = ("email",)	
filter_horizontal = ("groups", "user_permissions",)	
fieldsets = (	
(None, {"fields": ("email", "password")}),	
("Personal info", {"fields": ("favorite_topping",)}),	
("Permissions", {"fields": ("is_active", "is_staff", "is_superuser", "groups", "user_permissions")}),	
("Important dates", {"fields": ("last_login",)}),	
)	
add_fieldsets = ((None, {	
"classes": ("wide",),	
"fields": ("email", "favorite_topping", "password1", "password2")}), )
Option 3 - Let Admin
show your data. (2)
	


Implement your forms
class TwoScoopsUserCreationForm(UserCreationForm):	
"""A form for creating new users. Includes all the required	
fields, plus a repeated password."""	
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)	
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)	
#favorite_topping = forms.CharField( label='Faborate topping', widget=forms.TextInput)	

!

class Meta:	
model = TwoScoopsUser	
fields = ('email', 'favorite_topping')	
	
class TwoScoopsUserChangeForm(UserChangeForm):	
"""A form for updating users. Includes all the fields on	
the user, but replaces the password field with admin's	
password hash display field.	
"""	
password = ReadOnlyPasswordHashField()	

!

class Meta:	
model = TwoScoopsUser	
fields = ["email", "password", "favorite_topping", "is_active",	
"is_staff", "is_superuser", "groups", "user_permissions",	
"last_login"]
Two scoopsofdjango ch16   dealing with the user model
Thanks

More Related Content

PDF
Two scoopsofdjango common patterns for forms
PDF
Two Scoops of Django - Common Patterns for Forms
PPTX
Django: Advanced Models
PDF
Django design-patterns
PDF
날로 먹는 Django admin 활용
KEY
Django Admin: Widgetry & Witchery
PPTX
DJango admin interface
PPTX
Powerful Generic Patterns With Django
Two scoopsofdjango common patterns for forms
Two Scoops of Django - Common Patterns for Forms
Django: Advanced Models
Django design-patterns
날로 먹는 Django admin 활용
Django Admin: Widgetry & Witchery
DJango admin interface
Powerful Generic Patterns With Django

What's hot (20)

PDF
Forms, Getting Your Money's Worth
PPT
Common Pitfalls Experienced in Java
DOC
Validation
PPTX
Jsf lab
RTF
Html basics 11 form validation
 
PDF
Presentation technico-commercial-ruby-on-rails
PPTX
Xml operations in odoo
PDF
What's New in newforms-admin
PDF
Working With The Symfony Admin Generator
PDF
The Django Book, Chapter 16: django.contrib
PDF
Documentation For Tab Setup
PPT
Form demoinplaywithmysql
PPTX
HTML Form Part 1
PDF
PDF
Dig Deeper into WordPress - WD Meetup Cairo
KEY
Repensando o Desenvolvimento Web com Ruby on Rails
PDF
Django Admin (Python meeutp)
ODP
Ruby on rails
PDF
Django Bogotá. CBV
PDF
ALPHA Script - XML Model
Forms, Getting Your Money's Worth
Common Pitfalls Experienced in Java
Validation
Jsf lab
Html basics 11 form validation
 
Presentation technico-commercial-ruby-on-rails
Xml operations in odoo
What's New in newforms-admin
Working With The Symfony Admin Generator
The Django Book, Chapter 16: django.contrib
Documentation For Tab Setup
Form demoinplaywithmysql
HTML Form Part 1
Dig Deeper into WordPress - WD Meetup Cairo
Repensando o Desenvolvimento Web com Ruby on Rails
Django Admin (Python meeutp)
Ruby on rails
Django Bogotá. CBV
ALPHA Script - XML Model
Ad

Viewers also liked (7)

PDF
Dp carrier en
PDF
W make107
PPTX
SWOT ppt
PDF
SA02 - User and Password Management Techniques
PDF
2014 database - course 3 - PHP and MySQL
PDF
Hitachi ID Password Manager: Enrollment, password reset and password synchron...
PDF
Django Best Practices
Dp carrier en
W make107
SWOT ppt
SA02 - User and Password Management Techniques
2014 database - course 3 - PHP and MySQL
Hitachi ID Password Manager: Enrollment, password reset and password synchron...
Django Best Practices
Ad

Similar to Two scoopsofdjango ch16 dealing with the user model (20)

PDF
那些年,我用 Django Admin 接的案子
PDF
Class-based views with Django
PDF
Deep-dive into Django #1
PPT
Django Models
PPTX
DJ-02-Model-Single.pptx
PDF
Django Good Practices
PPTX
I regret nothing
PDF
Chapter 6 the django admin site
PDF
Django Heresies
PDF
PDF
Django tricks (2)
PDF
Ch9 .Best Practices for Class-Based Views
PPTX
Django Level Five Django Level Five.pptx
PPTX
Python oop third class
PPT
Django Forms: Best Practices, Tips, Tricks
PDF
Customizing the Django Admin
PPTX
Session 2 django material for training at baabtra models
PDF
What's new in Django 1.7
ODP
TangoWithDjango - ch8
PDF
Django Vs Rails
那些年,我用 Django Admin 接的案子
Class-based views with Django
Deep-dive into Django #1
Django Models
DJ-02-Model-Single.pptx
Django Good Practices
I regret nothing
Chapter 6 the django admin site
Django Heresies
Django tricks (2)
Ch9 .Best Practices for Class-Based Views
Django Level Five Django Level Five.pptx
Python oop third class
Django Forms: Best Practices, Tips, Tricks
Customizing the Django Admin
Session 2 django material for training at baabtra models
What's new in Django 1.7
TangoWithDjango - ch8
Django Vs Rails

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
cuic standard and advanced reporting.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Empathic Computing: Creating Shared Understanding
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
sap open course for s4hana steps from ECC to s4
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Digital-Transformation-Roadmap-for-Companies.pptx
Spectroscopy.pptx food analysis technology
cuic standard and advanced reporting.pdf
Unlocking AI with Model Context Protocol (MCP)
Mobile App Security Testing_ A Comprehensive Guide.pdf
Review of recent advances in non-invasive hemoglobin estimation
Approach and Philosophy of On baking technology
Encapsulation_ Review paper, used for researhc scholars
Empathic Computing: Creating Shared Understanding
MYSQL Presentation for SQL database connectivity
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
sap open course for s4hana steps from ECC to s4
The AUB Centre for AI in Media Proposal.docx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”

Two scoopsofdjango ch16 dealing with the user model

  • 1. Two Scoops of Django Chapter 16 - Dealing with the User Model Alfred
  • 2. When you check out your user model… or…
  • 3. How it authenticates… login Authentication Backends authenticate Model.User Model.UserManager pass User View you know, the best way is to trace the python source code. backends: contrib.auth.backends.py model: contrib.auth.models.py admin(user view): contrib.auth.admin.py • • •
  • 4. Basic From Django 1.5 onwards, the official preferred way to attach ForeignKey, OneToOneField, or ManyToManyField to User is as follows: from django.conf import settings from django.db import models ! class IceCreamStore(models.model): owner = models.OneToOneField(settings.AUTH_USER_MODEL) title = models.CharField(max_length=255)
  • 5. To customize your user table, there are 3 options...
  • 6. Warranty We suggest that you carefully try out option #1 below, as it should work with a minimum of effort. For Django 1.5-style custom User model definitions, we recommend option #2 and option #3 for new projects only. ! This is is because custom User model definitions for option #2 and option #3 adds new User tables to the database that will not have the existing project data. Unless project-specific steps are taken to address matters, migration means ORM connections to related objects will be lost.
  • 7. Option 1 - Link back You continue to use User (called preferably via django.contrib.auth.get user model()) and keep your related fields in a separate model (e.g. Profile). from django.conf import settings from django.db import models class UserProfile(models.Model): # If you do this you need to either have a post_save signal or # redirect to a profile_edit view on initial login. user = models.OneToOneField(settings.AUTH_USER_MODEL) favorite_ice_cream = models.CharField(max_length=30)
  • 8. Option 2 AbstractUser Choose this option if you like Django’s User model fields the way they are, but need extra fields !from django.contrib.auth.models import AbstractUser from django.db import models from django.utils.translation import ugettext_lazy as _ ! class KarmaUser(AbstractUser): karma = models.PositiveIntegerField(_("karma"), default=0, blank=True) #Setting.py AUTH_USER_MODEL = "profile.KarmaUser"
  • 9. Option 3 - Subclass AbstractBaseUser AbstractBaseUser is the bare-bones option with only 3 fields: password, last login, and is active. ! Let’s try it out with a custom User model for the Two Scoops project. Here are our requirements: We need an email address. We need to handle permissions per the traditional django.contrib.auth.models use of PermissionsMixin; providing standard behavior for the Django admin. We don’t need the first or last name of a user. We need to know their favorite ice cream topping. • • • •
  • 10. Option 3 - cont.(1) 1. Start from model. 2. Model.UserManager create User -> override it to create TwoScoopsUser 3. We need TwoScoopsUser! class TwoScoopsUser(AbstractBaseUser, PermissionsMixin): email = models.EmailField( verbose_name='email address', max_length=255, unique=True, db_index=True, ) favorite_topping = models.CharField(max_length=255) is_active = models.BooleanField(default=True) is_admin = models.BooleanField(default=False) is_staff = models.BooleanField(default=False) ! ! objects = TwoScoopsUserManager() USERNAME_FIELD = 'email' REQUIRED_FIELDS = ['favorite_topping']
  • 11. Option 3 - cont.(2) Implement usage functions… def get_full_name(self): return self.email ! def get_short_name(self): return self.email ! def __unicode__(self): return self.email ! def has_perm(self, perm, obj=None): return True ! def has_module_perms(self, app_label): return True
  • 12. Option 3 - cont. (3) Let’s write Model.UserManager to create TwoScoopsUser class TwoScoopsUserManager(BaseUserManager): def create_user(self, email, favorite_topping, password=None): """ Creates and saves a User with the given email, date of birth and password. """ ##Skip… user = self.model( email=TwoScoopsUserManager.normalize_email(email), favorite_topping=favorite_topping, ) ! user.set_password(password) user.save(using=self._db) return user
  • 13. Option 3 - cont. (4) Let’s write Model.UserManager to create TwoScoopsUser ! def create_superuser(self, email, favorite_topping, password): """ Creates and saves a superuser with the given email, date of birth and password. """ user = self.create_user(email, password=password, favorite_topping=favorite_topping ) user.is_admin = True user.is_staff = True user.is_superuser = True user.save(using=self._db) return user
  • 14. Option 3 - Let Admin show your data. Basically, override contrib.auth.admin … class TwoScoopsUserAdmin(UserAdmin): # The forms to add and change user instances form = TwoScoopsUserChangeForm add_form = TwoScoopsUserCreationForm ! ! # The fields to be used in displaying the User model. # These override the definitions on the base UserAdmin # that reference specific fields on auth.User. list_display = ("email", "is_staff", "favorite_topping") list_filter = ("is_staff", "is_superuser", "is_active", "groups") search_fields = ("email", "favorite_topping") ordering = ("email",) filter_horizontal = ("groups", "user_permissions",) fieldsets = ( (None, {"fields": ("email", "password")}), ("Personal info", {"fields": ("favorite_topping",)}), ("Permissions", {"fields": ("is_active", "is_staff", "is_superuser", "groups", "user_permissions")}), ("Important dates", {"fields": ("last_login",)}), ) add_fieldsets = ((None, { "classes": ("wide",), "fields": ("email", "favorite_topping", "password1", "password2")}), )
  • 15. Option 3 - Let Admin show your data. (2) Implement your forms class TwoScoopsUserCreationForm(UserCreationForm): """A form for creating new users. Includes all the required fields, plus a repeated password.""" password1 = forms.CharField(label='Password', widget=forms.PasswordInput) password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) #favorite_topping = forms.CharField( label='Faborate topping', widget=forms.TextInput) ! class Meta: model = TwoScoopsUser fields = ('email', 'favorite_topping') class TwoScoopsUserChangeForm(UserChangeForm): """A form for updating users. Includes all the fields on the user, but replaces the password field with admin's password hash display field. """ password = ReadOnlyPasswordHashField() ! class Meta: model = TwoScoopsUser fields = ["email", "password", "favorite_topping", "is_active", "is_staff", "is_superuser", "groups", "user_permissions", "last_login"]