模型中relationship的使用笔记
模型.PY
class User(db.Model):# __tablename__ = 'user1' #定义表名id = db.Column(db.Integer, primary_key=True, autoincrement=True)username = db.Column(db.String(10), nullable=True)password = db.Column(db.String(64), nullable=True)phone = db.Column(db.String(11), nullable=True, unique=True)rdatetime = db.Column(db.DateTime, default=datetime.now)isdelete = db.Column(db.Boolean, default=False)# 代码引用articles = db.relationship('Article', backref='user') # 反向引用comments = db.relationship('Comment', backref='users',secondary ='comment_user') # 反向引用 下方注释详解def __str__(self):return self.username
class Comment(db.Model):__tablename__ = 'comment'id = db.Column(db.Integer, primary_key=True, autoincrement=True)content = db.Column(db.String(255), nullable=False)cdatetime = db.Column(db.DateTime, default=datetime.now())# user_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False)article_id = db.Column(db.Integer, db.ForeignKey(Article.id), nullable=False)def __str__(self):return self.content
class Comment_user(db.Model):__tablename__ = 'comment_user'id = db.Column(db.Integer, primary_key=True, autoincrement=True)user_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False)comment_id = db.Column(db.Integer, db.ForeignKey(Comment.id), nullable=False)
articles = db.relationship('Article', backref='user') # 反向引用
comments = db.relationship('Comment', backref='users',secondary ='comment_user')
在视图中操作橙色方法1和红色方法2部分时',secondary ='comment_user',
这句话使得关系表中的数据自动添加,两种关系用法:
# user.comments.append(comment)
# comment1.users.append(user1)
# 评论文章
@article_bp.route('/comment', endpoint='comment', methods=['POST'])
def article_comment():
content = request.form.get('content')
user_id = request.form.get('author')
article_id = request.form.get('aid')
# print(content,user_id,article_id)
user = User.query.get(user_id)
comment = Comment()
comment.content = content
comment.article_id = article_id
user.comments.append(comment) #relationship中的关系
db.session.commit()
comment1 = Comment()
comment1.content='aaaaaa'
comment1.article_id = article_id
user1 = User.query.get(3)
comment1.users.append(user1)
db.session.add(comment)
db.session.commit()
url = url_for('article.detail') + '?aid=' + str(article_id)
return redirect(url)
视图.PY
@article_bp.route('/comment', endpoint='comment',methods=['POST'])
def article_comment():'''收: article_id content user_id创建Comment对象 保存数据操作关系表(comment_user): user_id comment_id送: 跳转到详情页'''content= request.form.get('content')user_id = request.form.get('author')article_id = request.form.get('aid')print(content,user_id,article_id)user = User.query.get(user_id)comment = Comment()comment.content = contentcomment.article_id = article_iduser.comments.append(comment) #user.comments相当于是一个列表#当调用comments 时直接等于进入到comment() 然后加入comment实例db.session.commit()url = url_for('article.detail')+'?aid='+str(article_id)return redirect(url)
user.comments.append(comment)
#user.comments相当于是一个列表
#当调用comments 时直接等于进入到comment() 然后加入comment实例
删除:
先找到对象
student.courses.remove(对象) ---》 自动删除关系表的记录