Flask中定義模型類的情況分別有哪些,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
在古縣等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需設(shè)計(jì)網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),全網(wǎng)營銷推廣,外貿(mào)網(wǎng)站建設(shè),古縣網(wǎng)站建設(shè)費(fèi)用合理。from datetime import datetime from werkzeug.security import generate_password_hash, check_password_hash from utils import constants from . import db class BaseModel(object): """模型基類,為每個(gè)模型補(bǔ)充創(chuàng)建時(shí)間與更新時(shí)間""" create_time = db.Column(db.DateTime, default=datetime.now) # 記錄的創(chuàng)建時(shí)間 update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now) # 記錄的更新時(shí)間 # 用戶收藏表,建立用戶與其收藏新聞多對(duì)多的關(guān)系 tb_user_collection = db.Table( "info_user_collection", db.Column("user_id", db.Integer, db.ForeignKey("info_user.id"), primary_key=True), # 新聞編號(hào) db.Column("news_id", db.Integer, db.ForeignKey("info_news.id"), primary_key=True), # 分類編號(hào) db.Column("create_time", db.DateTime, default=datetime.now) # 收藏創(chuàng)建時(shí)間 ) tb_user_follows = db.Table( "info_user_fans", db.Column('follower_id', db.Integer, db.ForeignKey('info_user.id'), primary_key=True), # 粉絲id db.Column('followed_id', db.Integer, db.ForeignKey('info_user.id'), primary_key=True) # 被關(guān)注人的id ) class User(BaseModel, db.Model): """用戶""" __tablename__ = "info_user" id = db.Column(db.Integer, primary_key=True) # 用戶編號(hào) nick_name = db.Column(db.String(32), unique=True, nullable=False) # 用戶昵稱 password_hash = db.Column(db.String(128), nullable=False) # 加密的密碼 mobile = db.Column(db.String(11), unique=True, nullable=False) # 手機(jī)號(hào) avatar_url = db.Column(db.String(256)) # 用戶頭像路徑 last_login = db.Column(db.DateTime, default=datetime.now) # 最后一次登錄時(shí)間 is_admin = db.Column(db.Boolean, default=False) signature = db.Column(db.String(512)) # 用戶簽名 gender = db.Column( # 訂單的狀態(tài) db.Enum( "MAN", # 男 "WOMAN" # 女 ), default="MAN") # 當(dāng)前用戶收藏的所有新聞 collection_news = db.relationship("News", secondary=tb_user_collection, lazy="dynamic") # 用戶收藏的新聞 # 用戶所有的粉絲,添加了反向引用followed,代表用戶都關(guān)注了哪些人 followers = db.relationship('User', secondary=tb_user_follows, primaryjoin=id == tb_user_follows.c.followed_id, secondaryjoin=id == tb_user_follows.c.follower_id, backref=db.backref('followed', lazy='dynamic'), lazy='dynamic') # 當(dāng)前用戶所發(fā)布的新聞 news_list = db.relationship('News', backref='user', lazy='dynamic') @property def password(self): raise AttributeError("當(dāng)前屬性不可讀") @password.setter def password(self, value): self.password_hash = generate_password_hash(value) def check_passowrd(self, password): return check_password_hash(self.password_hash, password) def to_dict(self): resp_dict = { "id": self.id, "nick_name": self.nick_name, "avatar_url": constants.QINIU_DOMIN_PREFIX + self.avatar_url if self.avatar_url else "", "mobile": self.mobile, "gender": self.gender if self.gender else "MAN", "signature": self.signature if self.signature else "", "followers_count": self.followers.count(), "news_count": self.news_list.count() } return resp_dict def to_admin_dict(self): resp_dict = { "id": self.id, "nick_name": self.nick_name, "mobile": self.mobile, "register": self.create_time.strftime("%Y-%m-%d %H:%M:%S"), "last_login": self.last_login.strftime("%Y-%m-%d %H:%M:%S"), } return resp_dict class News(BaseModel, db.Model): """新聞""" __tablename__ = "info_news" id = db.Column(db.Integer, primary_key=True) # 新聞編號(hào) title = db.Column(db.String(256), nullable=False) # 新聞標(biāo)題 source = db.Column(db.String(64), nullable=False) # 新聞來源 digest = db.Column(db.String(512), nullable=False) # 新聞?wù)? content = db.Column(db.Text, nullable=False) # 新聞內(nèi)容 clicks = db.Column(db.Integer, default=0) # 瀏覽量 index_image_url = db.Column(db.String(256)) # 新聞列表圖片路徑 category_id = db.Column(db.Integer, db.ForeignKey("info_category.id")) user_id = db.Column(db.Integer, db.ForeignKey("info_user.id")) # 當(dāng)前新聞的作者id status = db.Column(db.Integer, default=0) # 當(dāng)前新聞狀態(tài) 如果為0代表審核通過,1代表審核中,-1代表審核不通過 reason = db.Column(db.String(256)) # 未通過原因,status = -1 的時(shí)候使用 # 當(dāng)前新聞的所有評(píng)論 comments = db.relationship("Comment", lazy="dynamic") def to_review_dict(self): resp_dict = { "id": self.id, "title": self.title, "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"), "status": self.status, "reason": self.reason if self.reason else "" } return resp_dict def to_basic_dict(self): resp_dict = { "id": self.id, "title": self.title, "source": self.source, "digest": self.digest, "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"), "index_image_url": self.index_image_url, "clicks": self.clicks, } return resp_dict def to_dict(self): resp_dict = { "id": self.id, "title": self.title, "source": self.source, "digest": self.digest, "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"), "content": self.content, "comments_count": self.comments.count(), "clicks": self.clicks, "category": self.category.to_dict(), "index_image_url": self.index_image_url, "author": self.user.to_dict() if self.user else None } return resp_dict class Comment(BaseModel, db.Model): """評(píng)論""" __tablename__ = "info_comment" id = db.Column(db.Integer, primary_key=True) # 評(píng)論編號(hào) user_id = db.Column(db.Integer, db.ForeignKey("info_user.id"), nullable=False) # 用戶id news_id = db.Column(db.Integer, db.ForeignKey("info_news.id"), nullable=False) # 新聞id content = db.Column(db.Text, nullable=False) # 評(píng)論內(nèi)容 parent_id = db.Column(db.Integer, db.ForeignKey("info_comment.id")) # 父評(píng)論id parent = db.relationship("Comment", remote_side=[id]) # 自關(guān)聯(lián) like_count = db.Column(db.Integer, default=0) # 點(diǎn)贊條數(shù) def to_dict(self): resp_dict = { "id": self.id, "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"), "content": self.content, "parent": self.parent.to_dict() if self.parent else None, "user": User.query.get(self.user_id).to_dict(), "news_id": self.news_id, "like_count": self.like_count } return resp_dict class CommentLike(BaseModel, db.Model): """評(píng)論點(diǎn)贊""" __tablename__ = "info_comment_like" comment_id = db.Column("comment_id", db.Integer, db.ForeignKey("info_comment.id"), primary_key=True) # 評(píng)論編號(hào) user_id = db.Column("user_id", db.Integer, db.ForeignKey("info_user.id"), primary_key=True) # 用戶編號(hào) class Category(BaseModel, db.Model): """新聞分類""" __tablename__ = "info_category" id = db.Column(db.Integer, primary_key=True) # 分類編號(hào) name = db.Column(db.String(64), nullable=False) # 分類名 news_list = db.relationship('News', backref='category', lazy='dynamic') def to_dict(self): resp_dict = { "id": self.id, "name": self.name } return resp_dict
看完上述內(nèi)容,你們掌握Flask中定義模型類的情況分別有哪些的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。