Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Class Method4:07 with Kenneth Love
Sometimes it just doesn't make sense to make an instance of a class before we call a method on it. Python gives us a decorator named `@classmethod` that allows us to create an instance of the class from inside of it. Let's see how to use this to make user creation easier.
Let's talk more about
@classmethod. Let's make a class to represent an email.
class Email: to = None from = None subject = None content = None
If I want to make a new
email = Email() and then fill in the attributes. Assuming there's a
__init__() that handles setting the attributes, I can probably do that in one step.
But what if I want a method for immediately creating and sending the email? I either have to create an instance and then call
.send() on the instance or I need a
@classmethod way of generating one.
class Email: to = None from = None subject = None content = None @classmethod def create_and_send(cls, to, from, subject, content): cls(to=to, from=from, subject=subject, content=content).send()
This won't be a benefit to every class you create, but it's often a better way of approaching use cases where you don't need the class to hang around longer than needed to perform some action.
Okay, so for models.py we need to bring in our stuff from flask bcrypt. 0:02 So from flask.ext.bycrpt import generate_passord.hash 0:07 because that's what we're gonna have to generate, right? 0:12 Just to bring home this point, we're using flask.ext again. 0:18 All right, so now, let's add a method to our user model, 0:22 that we can use to generate new users. 0:29 Okay. 0:33 So let's say def create_user. 0:33 And I'm gonna pass in cls instead of self, and I'll explain why in just a minute. 0:37 We're gonna take a name, and let's call it username. 0:42 An email and a password. 0:47 And then we'll say is_admin, or admin, which is gonna be false. 0:49 Okay, so inside here, let's do a try. 0:54 And we'll do cls.create. 1:01 And we're gonna say username=username, 1:03 email=email, password=generate_password_hash. 1:07 Cuz we don't ever want to hold onto the password. 1:12 And then is_admin=admin. 1:15 All right, [SOUND] and then we're gonna catch this IntegrityError. 1:18 And the IntegrityError will be thrown if the user name of 1:26 the email are not actually unique. 1:29 So if somebody tries to write another email, or 1:32 an account with an email that's already used, or 1:34 an account where the user name is already used, it'll throw this IntegrityError. 1:36 And if that happens, we want to raise a ValueError. 1:39 Because one of the values is bad. 1:43 And we're gonna say User already exists. 1:46 Nothing crazy fancy. 1:50 Okay. 1:52 So, we're not done with this yet. 1:53 Because we have this problem here of, why did I say problem? 1:56 It's intentional we have this cls instead of self. 2:01 And if you think about it, it would be kind of weird 2:05 to have to create a user instance in order to call the create user method. 2:09 Because if it's a normal method it's an instance method. 2:15 It starts with self and you have to do it with an instance. 2:17 So I'd have to create a user instance, and 2:21 then use that user instance to call, create user, to make a new user instance. 2:23 That's weird. 2:29 So, what we're gonna do, 2:30 is we're actually gonna decorate this with a decorator named classmethod. 2:31 Classmethod describes a method 2:36 that belongs to a class that can create the class it belongs to. 2:40 Talk about this again. 2:44 The way it works if we don't have classmethod we have to create a user 2:45 instance to call create user which will create a user instance. 2:50 That's weird. 2:55 That's messy. 2:56 With classmethod on here and 2:56 with cls as this first argument, so the class is going to get passed into it. 2:59 It will create the user model instance when it runs this method and 3:05 then use that here. 3:11 So, cls here, ends up being user. 3:13 And cls here, ends up being user. 3:17 So it just calls user.create, on the model, which will create a new instance. 3:20 It's a little weird, I admit. 3:25 Think about it for a minute, it makes sense. 3:27 This cls is a pretty common convention that you'll see in Python code, 3:28 where you're referring back to the class that the method belongs to. 3:32 That makes me feel a lot better. 3:36 If you've ever gotten an email from a site with your password in plain text, 3:38 you know how badly people handle passwords. 3:41 Not us though, we never store the plain password at all. 3:44 Class methods are a great solution to building classes from nothing. 3:47 We'll be sure and always use our create user method whenever we need to 3:51 create a new user from now on. 3:53 We have a lot of our data model finished. 3:56 And we're well prepared for users to actually sign up and log into our site. 3:58 I think we're to a point where we can actually start 4:02 on the user facing parts of the site. 4:03 Let's do that in the next stage 4:05
You need to sign up for Treehouse in order to download course files.Sign up