Let's create a manager object for our
user profile. Because we're customizing
Django's user model we need to teach
django how to use our new user profile
model. We do this by creating a manager
object that goes with our user profile
model and we reference it here where we
define objects as the user profile
manager or user profile manager as the
objects. So let's create our user profile
manager we start by importing Django's
base user manager class this is the
class that django uses by default so
we're just going to use the same class
and we're going to build on top of it and
customize it to tell Django how to
use our new model correctly. So let's
import the base user manager
class by typing from django.contrib.auth.models import BaseUserManager
Okay so now that we've imported it let's
go ahead and build our manager class.
Start by typing class UserProfileManager
and let's inherit from the base
user manager that we just added. Let's
create a doc string that says "Helps Django
work with our custom user model." Alright
so you need to make sure that the user
profile manager name is the same name as
you gave here in objects so that needs
to match exactly. Alright so now let's
define a function that tells Django how
to create a user without custom user
model. So type def create underscore user
and we're going to add the parameters
self, name
or self, email, name and password and
we're going to add a doc string here
that says "Creates a new user profile
object." Okay so now we need to write the
logic that creates a new user in the
system. The first thing we're going to do
is check that they've provided an email
address. The email is a required field in
the system so we need to make sure that
if they try and call this without an
email address we show them an error
message. So we're going to check the
email exists and if
it doesn't exist so if not email and if
not email, so they didn't provide an
email, then raise a value error and
we'll give it the message users must
have an email address. Alright so when
you say if not, a field here, if you say
if not this variable name email then
what it does is it returns true if it's
none or false. So if they provide a blank
email or they put false in there then
it's going to raise this value error to
say the users must have an email address.
Aright so now we're going to do
something called normalizing the email
address. Okay so
normalized email address is a function
that normalizes the email address in the
system so that means it converts all of
the characters to lowercase or all of
the characters of the domain name to
lowercase so that way all the email
addresses are standardized in the system.
Do this by typing email equals self dot
normalized email email. Okay so now that
we normalized the email let's actually
create a new user profile object. Do this
by typing user equals
self dot model email equals email, so
we're going to create a new
model, a new version of ourselves
which is this user profile and we're
going to assign the email which is this
field here to the email which we've
normalized and the name to the name
which the user provided in the create
user function call. Alright so now the
last thing to do is to set the user's
password, so we're going to type user dot
set password password so we're going to
call this set password function of our
user profile model we're going to set
the password to the password they
provided here the reason we don't just
provide the password here is because
this set password function will encrypt
the password for us. So it basically
converts the string that they provided
as a password to a hash which is
stored in the database. So the users
actual clear text password is never
actually stored in the system. This is
best practice for creating a secure
system because if anyone does
compromised the database then they can't
actually see the users clear text
passwords all they can see is the user's
hash. Okay so now let's save the model by
typing user dot save and we're going to
use using equals self dot db means use
the same db that we created with the
user profile manager and we're going to
return the user object. Okay so that's
the create user function it checks that
they provided an email it normalizes the
email that they provided to make sure all
the domain characters are lowercase. It
creates a new user object, sets the
password in the object and it saves it
and then it returns the object. So the
second function we need to create on our
user profile manager is the function
that tells Django how to create a super
user. Okay so we're going to define a new
function called
create super user. So type def for
define create super user. Alright so
we're going to give it the parameters
self, email, name, and password. So more or
less the same here except we're not
going to put default as non on password
because we're going to require a
password for a super user. Alright so
that's create a doc string here that says
"Creates and saves a new super user with
given details." Alright so we can
actually use the create user function we
just created to create a standard user
and then change it to make that user a
super user. A super user is just a user
in the system that has full control over
everything so it's like an administrator
user. Let's call the function we created
and assign it to a variable called user.
So user equals self dot create user and
we're going to pass in the same fields
that we did here so we're going to pass
in the email the name and the password
that was provided when
we caused the create super user function.
Okay so after we've created a user we
need to assign two variables to the user.
We need to assign is super user or we
need to set is super user to true and is
staff to true. So type user dot is super user
equals true and user dot is staff
equals true. Alright so now that we've
done that we can save the user model so
user dot save using equal self
db and then finally return the user
object.
Okay so that's all we need to create in
the user profile manager to tell Django
how to use our custom user model. All it
needs is to create user so it knows how
to create a new standard user and the
create super user so it knows how to
create an administrator user in the
system. So that's how you create a user
profile manager in Django, thank you so
much for watching I hope you found this
video useful and I'll see you in the
next lesson.