用户模型
class User(db.Model):
__tablename__ = "users"
id = db.Column('user_id',db.Integer , primary_key=True)
username = db.Column('username', db.String(20), unique=True , index=True)
password = db.Column('password' , db.String(250))
posts = db.relationship('Post', backref = 'user', lazy = 'dynamic')
def __init__(self , username ,password ):
self.username = username
self.password = password
发布模型
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(140))
text = db.Column(db.String(2000))
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
def __init__(self, title, text):
self.title = title
self.text = text
现在我想添加评论模型,但不知道该怎么做。请在评论视图中帮助我。 评论应与用户和帖子相关联。
谢谢
答案 0 :(得分:0)
class Comment(db.Model):
__tablename__ = 'comments'
id = db.Column(db.Integer, primary_key = True)
text = db.Column(db.String(2000))
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
post_id = db.Column(db.Integer, db.ForeignKey('posts.post_id'))
def __init__(self, text):
self.text = text