flask双向映射语法

本文详细探讨了Flask框架中的双向映射语法,通过示例代码展示如何实现URL到视图函数以及视图函数到URL的双向绑定,帮助开发者更好地理解和运用这一特性。
摘要由CSDN通过智能技术生成

完整代码

import pymysql
from  flask import Flask
import  os
from flask_sqlalchemy import SQLAlchemy
app=Flask(__name__)#__name__当前文件
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统一资源匹配符;配置数据连接的参数#app.config返回类字典对象,里面用来存放当前app的配置
app.config["SQLALCHEMY_COMMIT_TEARDOWN"]=True
#sqlalchemy  commit  teardown请求结束后自动提交数据库修改
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"]=True
#如果设置成True(默认情况),flask_SQLALchemy 将会追踪对象的修改并且发送信号
models=SQLAlchemy(app)#关联sqlalchemy和flask应用
session=models.session()
class  BaseModel(models.Model):#学员表
    __abstract__=True#抽象表为True,代表当前类为抽象类,不会被实例化
    id=models.Column(models.Integer,primary_key=True,autoincrement=True)
    def  save(self):
        session.add(self)
        session.commit()
    def  delete_obj(self):#魔术方法
        session.delete(self)
        session.commit()
class  Students(BaseModel):#学员表
    __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关联关系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):#成绩表,课程、学员关联此表
    __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):#考勤表 关联学员
    __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"))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值