The first model that we're going to create for our project is the user
profile model
Out of the box Django comes with a default user model
that's used for the standard authentication system and also the
Django admin
We're going to override this model with our own custom model
that allows us to use an email address instead of the standard username that
comes with the Django default model
Best practice is to keep all of the models
files all of the database models in a file called models dot py within our app
so within our profiles API app load up the models dot py and you can see that
this is a template that is created by default by Django when we add the app to
our project
We're going to modify this file to include our user profile model
the first thing we need to do is import some additional imports at the top here
so underneath the from Django dot DB import models let's import the
abstract base user so type from Django dot contrib dot auth dot models import abstract base
user then underneath this we're going to import the permissions mixin from
Django dot contrib dot auth dot models import permissions mixin
These are the
standard base classes that you need to use when overriding or customizing the
default Django user model this is described in the Django official
documentation which I'll link to in the resources of this video
Underneath the
imports let's create a new class called user profile and inherit from the base
the abstract base user and the permissions mixin base classes so
add two lines below the imports and type class user profile with a capital u and a
capital P open brackets abstract base user and
permissions mixin
Then do : and add three double quotes here in a row and
type database model for users in the system and then end it with three double
quotes and this is the Python standard for writing doc strings which is
recommended under every class and function you add a doc string to
describe what this class and function does this helps remind you when you're
looking back at the code later on and also if you ever work with other
developers it helps explain what the purpose of the class is that you've
created
Okay so we've created a new class called
user profile and we have based our class on the abstract base user class and the
permissions mixin which means that we'll get all of the functionality from
the Django default user model but it allows us to customize it to our needs
okay below the doc string we can then define the various fields that we want
to provide on our model so if you type email equals models dot email field and
then with every email field you need to specify a max length and we'll set our
max length to 255 and in comma unique equals true
This says that we want an
email column on our user profile database table and we want that email
column to be an email field with a maximum length of 255 characters and we
want to make sure that it's unique in the system so we don't want to allow two
users to use the same email address every email address in the database must
be unique
Okay below the email we're going to specify the name field so type
name equals models dot char field max length equals 255 so we can store an email
address and a name field with each email
okay next we need to add a couple of fields for the permission system so the
first one is is underscore active equals models dot boolean field default equals
true so this is a field that we can use to determine if a user's profile is
activated or not by default we're going to set all of them to activated as
true but this allows us to deactivate users if we need to at some point in the
future and the boolean field is simply a field that holds a true or false value
this should actually be is active so let's make sure we spell this correctly
is active and below is active we want is underscore staff equals models dot boolean
field and this one we'll set default equals false so this determines if the
user is a staff user which is used to determine if they should have access to
the Django admin and things like that so by default all users in our system are
not going to be staff but if we need to create a staff user we can create this
and set is staff to true
Okay next we need to specify the model manager that
we're going to use for the objects and this is required for because we need to
use our custom user model with the Django CLI so Django needs to have a
custom model manager for the user model so it knows how to create users
and control users using the Django command line tools which I'll show you
in a future video so lets type objects must be objects with an S objects equals
and we'll create a user profile manager called user profile manager and add the
open and closed brackets here because we're going to pass in the class that
we're going to create the manager class and we haven't created this yet but we
are going to create this in a future video so just leave it there for now and
we'll create the user profile manager class in a bit
below this we need to add a couple more fields to our class and this is for it
again to work with the Django admin and also the Django authentication system so
we need to specify a username field and this is because we're overriding the
default username field which is normally called user name and we're replacing it
with our email field so this means that when we authenticate users instead of
them providing a username and password they're just going to provide their
email address and password
ok below this we're going to add a required fields
list
equals open the list and we're going to add name in the required fields so
this says that the username field is required by default so just by setting
it setting email and the user name that means that this is required and then you
have additional required fields and we want to say that at a minimum the user
must specify the username and their email address and their name
ok below this we're going to add a couple of functions which again are used
for Django to interact with our custom user model the first one is get full
name so we need to give Django the ability to retrieve the full name of the
user so if we type def get underscore for underscore name and in the arguments
because we are defining a function in a class we must specify self as the first
argument this is the default Python convention
Do : and again we're going to
add a doc string to our function here retrieve full name of user and then for
this function we're just going to return the name so return self dot name and this
defines a function that when you call get full name on our model object it
will return the full name of the user you're getting the name from
Below this we need
to specify get short name so def get short name self and then in the doc
string retrieve short name of user
and then we'll also return self dot name so
we're literally just specifying these functions so that we can use integrate
our custom user model with other components in Django and because we
don't have a way to specify a shorter name we're just going to return the same
name as the full name and the last name because we've kind of merged them both
into one single name field here
Okay finally we need to specify the string
representation of our model now this is the item that we want to return when we
convert a user profile object to a string in Python you do that by typing
def underscore underscore STR underscore underscore self and we'll do a doc
string here as well return string representation of our user and the
string representation will use is the email address so we'll just do return
self dot email now this is required for all or it's not required but this is
recommended for all Django models because otherwise when you convert it to a
string it won't necessarily be a meaningful output so in order to
customize how you convert this to a string you want to specify this function
here and return the field that you want to use to identify this model if you're
just reading it in the Django admin or in some Python code where you print it
so you'll see this in action later when we look at the Django admin and you'll
see all the users listed by their email address
okay so make sure to
save that file that's our first model that we've added to our project