WEBVTT
Kind: captions
Language: en

00:00:00.089 --> 00:00:04.980
The first model that we're going to
create for our project is the user

00:00:04.980 --> 00:00:07.120
profile model

00:00:07.120 --> 00:00:10.650
Out of the box
Django comes with a default user model

00:00:10.650 --> 00:00:14.040
that's used for the standard
authentication system and also the

00:00:14.040 --> 00:00:15.440
Django admin

00:00:15.440 --> 00:00:18.900
We're going to override
this model with our own custom model

00:00:18.900 --> 00:00:23.850
that allows us to use an email address
instead of the standard username that

00:00:23.850 --> 00:00:27.400
comes with the Django default model

00:00:29.140 --> 00:00:32.720
Best
practice is to keep all of the models

00:00:32.720 --> 00:00:38.800
files all of the database models in a
file called models dot py within our app

00:00:38.800 --> 00:00:43.739
so within our profiles API app load up
the models dot py and you can see that

00:00:43.739 --> 00:00:49.320
this is a template that is created by
default by Django when we add the app to

00:00:49.320 --> 00:00:51.100
our project

00:00:51.100 --> 00:00:54.920
We're going to modify this
file to include our user profile model

00:00:54.920 --> 00:00:59.850
the first thing we need to do is import
some additional imports at the top here

00:00:59.850 --> 00:01:05.250
so underneath the from Django dot DB import models let's import the

00:01:05.250 --> 00:01:17.630
abstract base user so type from Django dot contrib dot auth dot models import abstract base

00:01:17.630 --> 00:01:24.210
user then underneath this we're going to
import the permissions mixin from

00:01:24.210 --> 00:01:33.480
Django dot contrib dot auth dot models import
permissions mixin

00:01:33.480 --> 00:01:34.420
These are the

00:01:34.439 --> 00:01:39.570
standard base classes that you need to
use when overriding or customizing the

00:01:39.570 --> 00:01:44.700
default Django user model this is
described in the Django official

00:01:44.700 --> 00:01:49.460
documentation which I'll link to in the
resources of this video

00:01:50.780 --> 00:01:51.659
Underneath the

00:01:51.659 --> 00:01:58.590
imports let's create a new class called
user profile and inherit from the base

00:01:58.590 --> 00:02:04.189
the abstract base user and the
permissions mixin base classes so

00:02:04.189 --> 00:02:12.080
add two lines below the imports and type
class user profile with a capital u and a

00:02:12.080 --> 00:02:17.760
capital P
open brackets abstract base user and

00:02:17.770 --> 00:02:21.340
permissions mixin

00:02:21.340 --> 00:02:26.240
Then do :
and add three double quotes here in a row and

00:02:26.240 --> 00:02:32.780
type database model for users in the
system and then end it with three double

00:02:32.780 --> 00:02:39.080
quotes and this is the Python standard
for writing doc strings which is

00:02:39.080 --> 00:02:43.640
recommended under every class and
function you add a doc string to

00:02:43.640 --> 00:02:48.470
describe what this class and function
does this helps remind you when you're

00:02:48.470 --> 00:02:51.650
looking back at the code later on and
also if you ever work with other

00:02:51.650 --> 00:02:56.120
developers it helps explain what the
purpose of the class is that you've

00:02:56.120 --> 00:02:58.280
created

00:02:58.280 --> 00:03:01.190
Okay so we've created a new class called

00:03:01.190 --> 00:03:07.670
user profile and we have based our class
on the abstract base user class and the

00:03:07.670 --> 00:03:12.110
permissions mixin which means that
we'll get all of the functionality from

00:03:12.110 --> 00:03:18.580
the Django default user model but it allows
us to customize it to our needs

00:03:18.580 --> 00:03:24.800
okay below the doc string we can then
define the various fields that we want

00:03:24.800 --> 00:03:33.820
to provide on our model so if you type
email equals models dot email field and

00:03:33.820 --> 00:03:39.500
then with every email field you need to
specify a max length and we'll set our

00:03:39.500 --> 00:03:46.000
max length to 255 and in comma unique
equals true

00:03:46.000 --> 00:03:47.360
This says that we want an

00:03:47.360 --> 00:03:53.210
email column on our user profile
database table and we want that email

00:03:53.210 --> 00:03:58.760
column to be an email field with a
maximum length of 255 characters and we

00:03:58.760 --> 00:04:02.270
want to make sure that it's unique in
the system so we don't want to allow two

00:04:02.270 --> 00:04:07.460
users to use the same email address
every email address in the database must

00:04:07.460 --> 00:04:09.900
be unique

00:04:09.900 --> 00:04:13.790
Okay below the email we're
going to specify the name field so type

00:04:13.790 --> 00:04:22.910
name equals models dot char field max length
equals 255 so we can store an email

00:04:22.910 --> 00:04:26.690
address and a name field with each
email

00:04:26.690 --> 00:04:32.870
okay next we need to add a couple of
fields for the permission system so the

00:04:32.870 --> 00:04:41.210
first one is is underscore active equals
models dot boolean field default equals

00:04:41.210 --> 00:04:46.430
true so this is a field that we can use
to determine if a user's profile is

00:04:46.430 --> 00:04:51.260
activated or not by default we're going
to set all of them to activated as

00:04:51.260 --> 00:04:55.760
true but this allows us to deactivate
users if we need to at some point in the

00:04:55.760 --> 00:05:03.430
future and the boolean field is simply a
field that holds a true or false value

00:05:04.260 --> 00:05:09.660
this should actually be is active so
let's make sure we spell this correctly

00:05:09.660 --> 00:05:18.320
is active and below is active we want is
underscore staff equals models dot boolean

00:05:18.320 --> 00:05:24.260
field and this one we'll set default
equals false so this determines if the

00:05:24.260 --> 00:05:29.390
user is a staff user which is used to
determine if they should have access to

00:05:29.390 --> 00:05:35.210
the Django admin and things like that so
by default all users in our system are

00:05:35.210 --> 00:05:39.560
not going to be staff but if we need to
create a staff user we can create this

00:05:39.560 --> 00:05:43.560
and set is staff to true

00:05:43.560 --> 00:05:47.930
Okay next we
need to specify the model manager that

00:05:47.930 --> 00:05:53.390
we're going to use for the objects and
this is required for because we need to

00:05:53.390 --> 00:05:59.200
use our custom user model with the
Django CLI so Django needs to have a

00:05:59.200 --> 00:06:06.200
custom model manager for the user
model so it knows how to create users

00:06:06.200 --> 00:06:10.550
and control users using the Django
command line tools which I'll show you

00:06:10.550 --> 00:06:19.340
in a future video so lets type objects
must be objects with an S objects equals

00:06:19.340 --> 00:06:26.570
and we'll create a user profile manager
called user profile manager and add the

00:06:26.570 --> 00:06:31.910
open and closed brackets here because
we're going to pass in the class that

00:06:31.910 --> 00:06:36.440
we're going to create the manager class
and we haven't created this yet but we

00:06:36.440 --> 00:06:39.860
are going to create this in a future
video so just leave it there for now and

00:06:39.860 --> 00:06:43.240
we'll create the user profile manager class in a bit

00:06:43.240 --> 00:06:50.270
below this we need to add a couple more
fields to our class and this is for it

00:06:50.270 --> 00:06:56.210
again to work with the Django admin and
also the Django authentication system so

00:06:56.210 --> 00:07:02.390
we need to specify a username field and
this is because we're overriding the

00:07:02.390 --> 00:07:06.320
default username field which is normally
called user name and we're replacing it

00:07:06.320 --> 00:07:11.780
with our email field so this means that
when we authenticate users instead of

00:07:11.780 --> 00:07:14.540
them providing a username and password
they're just going to provide their

00:07:14.540 --> 00:07:17.740
email address and password

00:07:17.740 --> 00:07:20.840
ok below this we're going to add a required fields

00:07:20.840 --> 00:07:23.700
list

00:07:23.700 --> 00:07:29.720
equals open the list and we're
going to add name in the required fields so

00:07:29.720 --> 00:07:34.610
this says that the username field is
required by default so just by setting

00:07:34.610 --> 00:07:38.840
it setting email and the user name that
means that this is required and then you

00:07:38.840 --> 00:07:42.920
have additional required fields and we
want to say that at a minimum the user

00:07:42.920 --> 00:07:50.050
must specify the username and their email address and their name

00:07:50.050 --> 00:07:55.400
ok below this we're going to add a
couple of functions which again are used

00:07:55.400 --> 00:08:01.040
for Django to interact with our custom
user model the first one is get full

00:08:01.040 --> 00:08:05.450
name so we need to give Django the
ability to retrieve the full name of the

00:08:05.450 --> 00:08:13.850
user so if we type def get underscore for
underscore name and in the arguments

00:08:13.850 --> 00:08:20.750
because we are defining a function in a
class we must specify self as the first

00:08:20.750 --> 00:08:24.940
argument this is the default Python
convention

00:08:24.940 --> 00:08:26.870
Do : and again we're going to

00:08:26.870 --> 00:08:36.140
add a doc string to our function here
retrieve full name of user and then for

00:08:36.140 --> 00:08:43.610
this function we're just going to return
the name so return self dot name and this

00:08:43.610 --> 00:08:48.170
defines a function that when you call
get full name on our model object it

00:08:48.170 --> 00:08:52.743
will return the full name of the user you're
getting the name from

00:08:52.743 --> 00:08:53.990
Below this we need

00:08:53.990 --> 00:09:01.460
to specify get short name so def get
short name self and then in the doc

00:09:01.460 --> 00:09:06.520
string retrieve short name of user

00:09:06.520 --> 00:09:11.660
and then we'll also return self dot name so

00:09:11.660 --> 00:09:17.000
we're literally just specifying these
functions so that we can use integrate

00:09:17.000 --> 00:09:21.050
our custom user model with other
components in Django and because we

00:09:21.050 --> 00:09:25.100
don't have a way to specify a shorter
name we're just going to return the same

00:09:25.100 --> 00:09:29.029
name as the full name and the last name
because we've kind of merged them both

00:09:29.029 --> 00:09:32.660
into one single name field here

00:09:32.660 --> 00:09:35.570
Okay
finally we need to specify the string

00:09:35.570 --> 00:09:42.620
representation of our model now this is
the item that we want to return when we

00:09:42.620 --> 00:09:48.020
convert a user profile object to a
string in Python you do that by typing

00:09:48.020 --> 00:09:56.480
def underscore underscore STR underscore
underscore self and we'll do a doc

00:09:56.480 --> 00:10:04.550
string here as well return string
representation of our user and the

00:10:04.550 --> 00:10:09.350
string representation will use is the
email address so we'll just do return

00:10:09.350 --> 00:10:15.620
self dot email now this is required for
all or it's not required but this is

00:10:15.620 --> 00:10:20.300
recommended for all Django models
because otherwise when you convert it to a

00:10:20.300 --> 00:10:24.860
string it won't necessarily be a
meaningful output so in order to

00:10:24.860 --> 00:10:29.270
customize how you convert this to a
string you want to specify this function

00:10:29.270 --> 00:10:33.860
here and return the field that you want
to use to identify this model if you're

00:10:33.860 --> 00:10:39.589
just reading it in the Django admin or
in some Python code where you print it

00:10:39.589 --> 00:10:44.660
so you'll see this in action later when
we look at the Django admin and you'll

00:10:44.660 --> 00:10:49.143
see all the users listed by
their email address

00:10:49.143 --> 00:10:51.050
okay so make sure to

00:10:51.050 --> 00:10:57.610
save that file that's our first model
that we've added to our project

