Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Terrell Stewart
Courses Plus Student 4,228 PointsWhat exactly is the functionality of id in User.id and user_id
I understand that User refers to the class but where does the id attribute come from and what does it do? As well as the user_id
@login_manager.user_loader
def load_user(user_id):
try:
models.User.get(models.User.id==user_id)
2 Answers

Steven Parker
242,770 PointsYou didn't link to or mention any particular course or video where you found this, but based on the code snippet the id is clearly, as you say yourself, an attribute of the "User" class. An attribute is basically a local variable built into the class. It's probably just one of several bits of info that the class maintains for the user.
In your code sample, user_id is just a parameter that will be passed to the function.
Unrelated to your question, the way that get method is being used seems a bit odd, but it's actual function would not be clear without seeing more of the code.

James J. McCombie
Python Web Development Techdegree Graduate 21,199 PointsHello Terrell,
Had similar questions about that part of the flask tutorial myself.
the decorated function:
@login_manager.user_loader def user_loader(userid):
takes an integer, and via the model.User.get(), returns the user object by finding the row in the database whose primary key (id) is equal to the userid passed in. The primary key is set automatically when using peewee, I guess as the rows are added to the table they get the next integer in sequence as their id, unless its stated explicitally in the Model class. This much I am fairly sure on.
But I am unsure also on how this function is called and where the userid parameter is gotten from to be passed into the function.
Remember the UserMixin class? this gives access to these four methods:
is_authenticated() is_active() is_anonymous() get_id()
These are all needed for Flask-Login to work, UserMixin gives them to us to use without having to define them explicitly in the classes ourselves. The is_authenticated() method is required so that the @login_required decorator works, it simply returns True or False if authenticated which is then used by the @login_required decorator. This does work on my app, so is_authenticated() must be running behind the scenes since in the tutotial we inherit from UserMixin by adding it into our User class inheritance chain.
so therefore get_id() must also be running behind the scenes. UserMixin provides default implementations of these four methods in the User class created, by doing class User(UserMixin, Model). You could override the get_id() default by:
class User(UserMixin, Model):
""" your existing attributes and methods"""
def get_id(self):
return self.id
I think that is roughly what the get_id() method would do, you could set it to say self.email, or self.username, and it would return them. In the default case then, the get_id() UserMixin method provides id of the user instance, which I think is just the primary key from the row in the User table, as a string (this must mean that peewee also stores the id's as strings)
Much like @login_required uses what is returned from the is_authenticated() method, the decorator @login_manager.user_loader defines where the function it decorates gets its parameter from, using the returned self.id from the get_id() method on a particular, then loading that users object by getting its record from the database. It reloads the user object from the user ID stored in the session, I believe this is important fro example if a user is navigating across pages. The session gets the unique ID from the get_id() method that inheriting from UserMixin gives the classes.
When we login a user using the login view, this is checking the form data against the rows in the database table User, if matched then as the function is defined it calls the login_user function we import from flask-login. During this step must be where the ID comes from as it can be pulled from the row when the user instance is passed to the login_user function.
This made me think also of the current_user, this must just be any client accessing the server our app is running on. if this client logs in then current_user is then authenticated, and has an ID, as described above, and is given the user object from the user_loader. Remember we also set the current_user to the global variable, this means this is available to all the views in the app, along with all its attributes and methods.
Hope that helps, I would not trust it too much as I am pretty green at this, but maybe you can confirm for yourself, and let me know if you figure out if any of what I said is way off.
Better still maybe someone like Kenneth's skill level could wade in.....?