July 2018
Intermediate to advanced
324 pages
8h 14m
English
We are going to use Flask-SQLAlchemy to create a todo model. It will have a relationship with the User model with a backward reference, so that we can query the todo data that's related to the User model.
The following is the code snippet for the todo model.
File—todo/models.py:
from datetime import datetimefrom app import dbfrom app.config.models import BaseModelclass Todo(db.Model, BaseModel): __tablename__ = 'todo' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128)) is_completed = db.Column(db.Boolean, default=False) created_by = db.Column(db.String(64), db.ForeignKey('user.email')) user = db.relationship('User', backref=db.backref('todos', lazy=True)) def __init__(self, title, created_by=None, created_at=None): ...Read now
Unlock full access