Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # from flaskblog folder in __init__.py
- from enum import unique
- from datetime import datetime
- from app import db
- from sqlalchemy import Column, Integer, String
- from flask_login import UserMixin
- from flask_login import LoginManager
- # https://stackoverflow.com/questions/63231163/what-is-the-usermixin-in-flask
- # one to many relationship between both databases
- class User(UserMixin, db.Model):
- # The primary key creates the one in an one to many relationship between both databases
- id = db.Column(db.Integer, primary_key=True)
- username = db.Column(db.String(80), unique=True, nullable=False)
- hashed_password = db.Column(db.String(128), nullable=False)
- email = db.Column(db.String(120), unique=True, nullable=False)
- # backref='user' connects to the user database. You name the backref= the same name as the database - the capital letter
- # Connects Post database and User database through the posts variable
- posts = db.relationship('Posts', backref='user', lazy=True)
- # what does this do?
- def __repr__(self):
- return '<User %r>' % self.username
- class Posts(UserMixin, db.Model):
- id = db.Column(db.Integer, primary_key=True)
- title = db.Column(db.String(120), unique=True, nullable=False)
- content = db.Column(db.String(120), unique=True, nullable=False)
- date_posted = db.Column(db.DateTime, nullable=False,
- default=datetime.utcnow)
- # The foreign key creates the many in an one to many relationship between both databases
- # user.id represents the id from the User database and the posts variable specifically backref='user'
- user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
- # what does this do?
- def __repr__(self):
- return '<Posts %r>' % self.username
- from app import login_manager
- # Use User.query.get instead of User.get because of sqlalchemy
- # what is user_id
- @login_manager.user_loader
- def load_user(user_id):
- return User.query.get(user_id)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement