class Comment(db.Model): """ author: 作者 email: 电子邮件 site: 站点 body: 正文 from_admin: 是否是管理员评论 reviewed: 是否通过审核 timestamp: 时间戳 """ id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(30)) email = db.Column(db.String(254)) site = db.Column(db.String(255)) body = db.Column(db.Text) from_admin = db.Column(db.Boolean, default=False) reviewed = db.Column(db.Boolean, default=False) timestamp = db.Column(db.DateTime, default=datetime.utcnow) # post表的id值 post_id = db.Column(db.Integer, db.ForeignKey('post.id')) replied_id = db.Column(db.Integer, db.ForeignKey('comment.id')) post = db.relationship('Post', back_populates='comments') replies = db.relationship('Comment', back_populates='replied', cascade='all, delete-orphan') replied = db.relationship('Comment', back_populates='replies', remote_side=[id])
class Post(db.Model): """生成文章表的类 规定文章表的各个属性 Attributes: id:文章的 id,主键 title:文章的标题,限制长度60 body:文章的内容 timestamp:文章的创建时间,默认是当前时间 can_coment:文章是否可以评论 category_id:文章的分类id,外键 category:设置和分类的中间表的名称 coments:设置和评论的中间表的名称,规定删除文章时,删除文章下的所有评论 """ id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow) can_comment = db.Column(db.Boolean, default=True) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='posts') comments = db.relationship('Comment', back_populates='post', cascade='all, delete-orphan')
class Comment(db.Model): """评论模型""" id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(30)) email = db.Column(db.String(254)) site = db.Column(db.String(255)) body = db.Column(db.Text) from_admin = db.Column(db.Boolean, default=False) reviewed = db.Column(db.Boolean, default=False, comment='评论是否通过审核') timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) replied_id = db.Column(db.Integer, db.ForeignKey("comment.id")) # 设置外键指向自身id post_id = db.Column(db.Integer, db.ForeignKey('post.id')) post = db.relationship('Post', back_populates='comments') # 集合关系属性replies 中的cascade参数设为all,当父评论被删除,所有子评论也删除 replies = db.relationship('Comment', back_populates='replied', cascade='all,delete-orphan') # 将remote_side参数设为id字段, # 把id字段定义为关系的远程侧(Remote Side),replied_id相应的变为本地侧(Local Side) replied = db.relationship('Comment', back_populates='replies', remote_side=[id])
class Comment(db.Model): """每条博客的评论""" id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(20)) email = db.Column(db.String(254)) # site表示发布comment的作者的个人网站 site = db.Column(db.String(255)) body = db.Column(db.Text) # 是否是自己评论 from_admin = db.Column(db.Boolean, default=False) timestamp = db.Column(db.DateTime, default=datetime.now(), index=True) # Post:Comment=One:Many 一对多关系 Comment中含有Post的外键 post_id = db.Column(db.Integer, db.ForeignKey('post.id')) post = db.relationship('Post', back_populates='comments') # 评论审查是否通过 reviewed = db.Column(db.Boolean, default=False) # 得到评论的回复列表,此字段在项目中没有用到 # replies = db.relationship('Comment', lazy='select', # backref=db.backref('replied', remote_side=[id]), # cascade='all,delete-orphan') replies = db.relationship('Comment', back_populates='replied', cascade='all, delete-orphan') # 还有一种特殊的评论,这种评论是对评论的评论,我暂且叫他回复 # 假如还有一个表叫Reply # Comment:Reply=One:Many 回复同时也是一种特殊的评论,引用自己的主键,叫自关联 # replied_id:表示对那个comment进行的回复 replied_id = db.Column(db.Integer, db.ForeignKey('comment.id')) # 根据post.html的理解,此字段表示当前reply是对那个comment的回答,这个是特殊的comment replied = db.relationship('Comment', back_populates='replies', remote_side=[id])
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) # 从文章标题生成 slug = db.Column(db.String(240)) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow) # 是否可以评论 can_comment = db.Column(db.Boolean, default=True) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='posts') # 评论 comments = db.relationship('Comment', backref='post', cascade='all,delete-orphan') def to_json(self): dict = self.__dict__ dict['category'] = self.category.name dict['comments'] = len(self.comments) if "_sa_instance_state" in dict: del dict["_sa_instance_state"] dict['timestamp'] = dict['timestamp'].strftime('%Y-%m-%d %H:%M:%S') return dict
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='posts') comments = db.relationship('Comment', back_populates='post', cascade='all')
class Article(db.Model): __tablename__ = 'article' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) blogger_id = db.Column(db.Integer, db.ForeignKey('blogger.id')) blogger = db.relationship('Blogger', backref='article') comments = db.relationship('Comment', backref='article', cascade='all, delete-orphan')
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow) can_comment = db.Column(db.Boolean, default=True) # 和Category的双向关系 category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='posts') # 和Comment的双向关系 comments = db.relationship('Comment', back_populates='post', cascade='all, delete-orphan') # 设置级联删除,即文章删除之后对应的评论也一并删除
class Post(db.Model): id = db.Column(db.INTEGER, primary_key=True) title = db.Column(db.String(100)) body = db.Column(db.TEXT) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) can_comment = db.Column(db.Boolean, default=True) category_id = db.Column(db.INTEGER, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='posts') comments = db.relationship('Comment', back_populates='post', cascade='all, delete-orphan')
class Post(db.Model): #文章模型 __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow) can_comment = db.Column(db.Boolean, default=True) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='posts') #分类和文章之间具有一对多的关系 comments = db.relationship('Comment', back_populates='post', cascade='all,delete-orphan')#级联删除 def __repr__(self): return '<Post {}>'.format(self.title)
class Subcategory(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(15), ) exegesis = db.Column(db.String(30)) frequency = db.Column(db.Integer, index=True, default=0) # 次数 disable = db.Column(db.Boolean, index=True, default=False) auto_id = db.Column(db.Integer, db.ForeignKey('auto.id')) major_id = db.Column(db.Integer, db.ForeignKey('major.id')) major = db.relationship('Major', back_populates='subcategorys') picture = db.relationship('Picture', back_populates='subcategorys', cascade='all, delete') timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True)
class Category(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) posts = db.relationship('Post', back_populates='category') experiments = db.relationship('Experiment', back_populates='category') def delete(self): default_category = Category.query.get(1) posts = self.posts[:] for post in posts: post.category = default_category db.session.delete(self) db.session.commit()
class Post(db.Model): __tablename__ = 'post' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) # 文章标题 body = db.Column(db.Text) # 正文 # 使用格林威治时间,不带时区 timestamp = db.Column(db.DateTime, default=datetime.utcnow) # 是否可以评论 can_comment = db.Column(db.Boolean, default=True) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='posts') # 建立双向关系(一post对多comments),并设置级联删除,当文章删除时,相应的评论也删除掉 comments = db.relationship('Comment', back_populates='post', cascade='all,delete-orphan')
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) can_comment = db.Column(db.Boolean, default=True) published = db.Column(db.Boolean, default=False) # 默认仅管理员可见 pinned = db.Column(db.Boolean, default=False) # 默认不置顶 category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='posts') comments = db.relationship('Comment', back_populates='post', cascade='all, delete-orphan') tags = db.relationship('Tag', secondary=association_table, back_populates='posts')
class Post(db.Model): """文章""" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow) can_comment = db.Column(db.Boolean, default=True) # 外键 category_id = db.Column(db.Integer, db.ForeignKey('category.id')) # 标量关系属性 category = db.relationship('Category', back_populates='posts') # 集合关系属性,设置级联删除,文章删除后,其评论会同时被删除 comments = db.relationship('Comment', back_populates='post', cascade='all,delete-orphan')
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60)) body = db.Column(db.Text) obperson = db.Column(db.Integer) author = db.Column(db.String(30)) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) # can_comment = db.Column(db.Boolean, default=True) reviewed = db.Column(db.Boolean, default=False) suggestion = db.Column(db.Text, default=None) admin_id = db.Column(db.Integer, db.ForeignKey('admin.id')) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) admin = db.relationship('Admin', back_populates='posts') category = db.relationship('Category', back_populates='posts')
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(50)) email = db.Column(db.String(254)) site = db.Column(db.String(254)) body = db.Column(db.Text) from_admin = db.Column(db.Boolean, default=False) reviewed = db.Column(db.Boolean, default=False) timestamp = db.Column(db.DateTime, default=datetime.utcnow) post_id = db.Column(db.Integer, db.ForeignKey('post.id')) post = db.relationship('Post', back_populates='comments') reply_id = db.Column(db.Integer, db.ForeignKey('comment.id')) reply = db.relationship('Comment', back_populates='replies', remote_side=[id]) replies = db.relationship('Comment', back_populates='reply', cascade='all')
class Category(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) # 集合关系属性 posts = db.relationship('Post', back_populates='category') pass
class Category(db.Model): """生成分类的类 规定了分类的各个属性及与文章的关系 Attributes: id:分类的id,主键 name: 分类的名称,长度限制30,不能重复 posts: 设置中间表名称 """ id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) posts = db.relationship('Post', back_populates='category') def delete(self): """删除分类 删除分类并将此分类下的文章移至默认分类 """ default_category = Category.query.get(1) posts = self.posts[:] for post in posts: post.category = default_category db.session.delete(self) db.session.commit()
class Category(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) posts = db.relationship('Post', back_populates='category') def __repr__(self): return f'<Category {self.name}>'
class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(16), unique=True) posts = db.relationship('Post', secondary=association_table, back_populates='tags') def delete(self): db.session.delete(self) db.session.commit()
class Category(db.Model): #文章的分类 __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) posts = db.relationship('Post', back_populates='category') def __repr__(self): return '<Category {}>'.format(self.name)
class Category(db.Model): """博客分类""" __tablename__ = 'category' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20), nullable=False, unique=True) # category.posts 知道category求category下所有的post # post.post_of_category 知道post,求post所在的category posts = db.relationship('Post', back_populates='category', cascade='all,delete-orphan')
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(30)) email = db.Column(db.String(254)) site = db.Column(db.String(255)) body = db.Column(db.Text) from_admin = db.Column(db.Boolean, default=False) reviewed = db.Column(db.Boolean, default=False) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) # 子评论与父评论连接起来,多对一关系,在多的一侧建立外键 replied_id = db.Column(db.Integer, db.ForeignKey('comment.id')) post_id = db.Column(db.Integer, db.ForeignKey('post.id')) post = db.relationship('Post', back_populates='comments') # 与子评论链接起来,当父评论被删除时,一起删除;属于Comment类 replies = db.relationship('Comment', back_populates='replied', cascade='all, delete-orphan') replied = db.relationship('Comment', back_populates='replies', remote_side=[id])
class Comment(db.Model): #文章的评论 __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(30)) email = db.Column(db.String(254)) site = db.Column(db.String(255)) body = db.Column(db.Text) from_admin = db.Column(db.Boolean, default=False) reviewed = db.Column(db.Boolean, default=False)#判断评论是否通过审核以防止不当评论 timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) post_id = db.Column(db.Integer, db.ForeignKey('post.id'))#文章和评论存在一对多关系 post = db.relationship('Post', back_populates='comments') replied_id = db.Column(db.Integer, db.ForeignKey('comment.id')) replied = db.relationship('Comment', back_populates='replies', remote_side=[id]) #父评论 replies = db.relationship('Comment', back_populates='replied', cascade='all') #多个回复 def __repr__(self): return '<Comment {}>'.format(self.author)
class Post(db.Model): """博客条目""" __tablename__ = 'post' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(60), ) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow(), index=True) can_comment = db.Column(db.Boolean, default=True) # Category:Post=One:Many 一对多关系 Post中含有Category的外键 category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='posts') # Post.comments 知道post求post下所有的comments # comment.comment_of_post 知道comment,求comment对应的post comments = db.relationship('Comment', back_populates='post', cascade='all,delete-orphan') def __str__(self) -> str: return self.title
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(30)) email = db.Column(db.String(254)) site = db.Column(db.String(255)) body = db.Column(db.Text) from_admin = db.Column(db.Boolean, default=False) # 是否是管理员的评论 reviewed = db.Column(db.Boolean, default=False) # 该评论是否过了审核 timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) post_id = db.Column(db.Integer, db.ForeignKey('post.id')) post = db.relationship('Post', back_populates='comments') replied_id = db.Column(db.Integer, db.ForeignKey('comment.id')) replied = db.relationship('Comment', back_populates='replies', remote_side=[id]) replies = db.relationship('Comment', back_populates='replied', cascade='all, delete-orphan')
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(30)) email = db.Column(db.String(254)) site = db.Column(db.String(255)) body = db.Column(db.Text) from_admin = db.Column(db.Boolean, default=False) reviewed = db.Column(db.Boolean, default=False) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) post_id = db.Column(db.Integer, db.ForeignKey('post.id')) post = db.relationship('Post', back_populates='comments') # 设置邻接关系,也就是内部的关联,内部的一对多 replied_id = db.Column(db.Integer, db.ForeignKey('comment.id')) replied = db.relationship('Comment', back_populates='replies', remote_side=[id]) # remote_side确定了这个字段为多的一侧 replies = db.relationship('Comment', back_populates='replied', cascade='all')
class Experiment(db.Model): id = db.Column(db.Integer, primary_key=True) specimen = db.Column(db.Integer) user = db.Column(db.String(30)) extensometer = db.Column(db.String(30)) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) can_edit = db.Column(db.Boolean, default=True) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='experiments')
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(30)) email = db.Column(db.String(254)) site = db.Column(db.String(255)) body = db.Column(db.Text) from_admin = db.Column(db.Boolean, default=False) # 是否是管理员的评论 reviewed = db.Column(db.Boolean, default=False) # 是否通过审核,以防止垃圾评论或不当评论 timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) # 和Post的双向关系 post_id = db.Column(db.Integer, db.ForeignKey('post.id')) post = db.relationship('Post', back_populates='comments') # 评论回复功能,并在获取某个评论时可以通过关系属性获得对应的回复,这样就可以在模板中显示出评论之间的对应关系 # 因为回复本身也是评论,如果在评论模型内建立层级关系,就可以在一个模型中表示评论和评论的回复 # 这种在同一个模型内部的一对多关系在SQLAlchemy中被称为邻接列表关系(Adjacency List Relationship) # 在Comment模型内部添加一个外键指向它自身,这样就得到一种层级关系:每个评论对象都可以包含多个自评论,即回复 replied_id = db.Column(db.Integer, db.ForeignKey('comment.id')) replies = db.relationship('Comment', back_populates='replied', cascade='all, delete-orphan') # 设置级联删除,即评论删除之后对应的回复也一并删除 replied = db.relationship('Comment', back_populates='replies', remote_side=[id])