flask双向映射语法
生活随笔
收集整理的這篇文章主要介紹了
flask双向映射语法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
完整代碼
import pymysql from flask import Flask import os from flask_sqlalchemy import SQLAlchemy app=Flask(__name__)#__name__當(dāng)前文件 BASE_DIR=os.path.abspath(os.path.dirname(__file__))#路徑 #app.config['SQLALCHEMY_DATABASE_URI']="sqlite:///"+os.path.join(BASE_DIR,"Demo.sqlite") #app.config["SQLALCHEMY_DATABASE_URI"]="mysql+pymysql://root:root@127.0.0.1:3306/student"+os.path.join(BASE_DIR,"MySQL") app.config["SQLALCHEMY_DATABASE_URI"]="mysql+pymysql://root:root@127.0.0.1:3306/student" #URI統(tǒng)一資源匹配符;配置數(shù)據(jù)連接的參數(shù)#app.config返回類字典對象,里面用來存放當(dāng)前app的配置 app.config["SQLALCHEMY_COMMIT_TEARDOWN"]=True #sqlalchemy commit teardown請求結(jié)束后自動提交數(shù)據(jù)庫修改 app.config["SQLALCHEMY_TRACK_MODIFICATIONS"]=True #如果設(shè)置成True(默認(rèn)情況),flask_SQLALchemy 將會追蹤對象的修改并且發(fā)送信號 models=SQLAlchemy(app)#關(guān)聯(lián)sqlalchemy和flask應(yīng)用 session=models.session() class BaseModel(models.Model):#學(xué)員表__abstract__=True#抽象表為True,代表當(dāng)前類為抽象類,不會被實(shí)例化id=models.Column(models.Integer,primary_key=True,autoincrement=True)def save(self):session.add(self)session.commit()def delete_obj(self):#魔術(shù)方法session.delete(self)session.commit() class Students(BaseModel):#學(xué)員表__tablename="students"name=models.Column(models.String(32))age=models.Column(models.Integer)gender=models.Column(models.Integer)#0,男;1女;2其他 class Course(BaseModel):#課程表__tablename = "course"label = models.Column(models.String(32))description=models.Column(models.Text)#雙向映射teachers=models.relationship(#relationship關(guān)聯(lián)關(guān)系relationship'Teachers',#映射類的名稱和類名一致#雙向映射backref='course'#表名和表名一致#教師類映射到課程表)#教師類映射到該表#雙向映射語法:字段名=models.relationship(#"映射的類名",#backref="本表名") class Stu_Cou(BaseModel):#課程表__tablename = "stu_cou"course_id = models.Column(models.Integer, models.ForeignKey("course.id"))student_id = models.Column(models.Integer, models.ForeignKey("students.id")) class Grade(BaseModel):#成績表,課程、學(xué)員關(guān)聯(lián)此表__tablename = "grade"id = models.Column(models.Integer, primary_key=True, autoincrement=True)grade=models.Column(models.Float)course_id=models.Column(models.Integer,models.ForeignKey("course.id"))student_id = models.Column(models.Integer, models.ForeignKey("students.id")) class Attendance(BaseModel):#考勤表 關(guān)聯(lián)學(xué)員__tablename = "attendance"id = models.Column(models.Integer, primary_key=True, autoincrement=True)student_id = models.Column(models.Integer, models.ForeignKey("students.id"))att_time=models.Column(models.Date)status = models.Column(models.Integer,default=1)#0遲到;1正常出勤;2早退;3請假;4曠課 class Teachers(BaseModel):#教師表__tablename = "teachers"id = models.Column(models.Integer, primary_key=True, autoincrement=True)name = models.Column(models.String(32))age = models.Column(models.Integer)gender = models.Column(models.Integer) # 0,男;1女;2其他course_id = models.Column(models.Integer, models.ForeignKey("course.id"))總結(jié)
以上是生活随笔為你收集整理的flask双向映射语法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: flask框架创建数据库定义字段类型和字
- 下一篇: flask模块划分