《FlaskWeb 开发实战:⼊门、进阶与原理解析》-李辉笔记代码学习和注释(第
五章-数据库)
⼀、ORM
ORM⽤类来定义表,类的实例化代表了表的⼀整⾏,那么整个表就是类的实例化的LIST.
⼆、在表单中实现增删改查代码样例
删(⾥⾯包括了⽤主键查表的⾏)
改(直接覆盖掉就⾏了)
查@ute('/new', methods =['GET', 'POST'])def  new_note ():#是⼀个⽐较典范的程序    form = NewNoteForm ()    if  form .validate_on_submit ():        body = form .body .data        note = Note (body =body )        db .session .add (note )        db .session mit ()        flash ('Your note is saved.')        return  redirect (url_for ('index'))    return  render_template ('new_note.html', form =form )
1
2
3
4
5
6
7
8
9
1011
12
ute('/delete/<int:note_id>', methods =['POST'])def  delete_note (note_id ):    form = DeleteNoteForm ()    if  form .validate_on_submit ():        note = Note .query .get (note_id )#通过id 先到        db .session .delete (note )#然后直接删除掉        db .session mit ()        flash ('Your note is deleted.')    else :        abort (400)    return  redirect (url_for ('index'))
1
2
3
4
5
6
7
8
9
数据库学习入门书籍10
11
12
ute('/edit/<int:note_id>', methods =['GET', 'POST'])def  edit_note (note_id ):    form = EditNoteForm ()    note = Note .query .get (note_id )    if  form .validate_on_submit ():        note .body = form .body .data        db .session mit ()        flash ('Your note is updated.')        return  redirect (url_for ('index'))    form .body .data = note .body  # preset form input's value    return  render_template ('edit_note.html', form =form )
1
2
3
4
5
6
7
8
9
10
11
12
三、表关系
⼀对多
⼀对⼀
多对多note = Note .query .get (note_id )#查⼀⾏notes = Note .query .all ()#查全部
1
2
3# one to many class  Author (db .Model ):    id  = db .Column (db .Integer , primary_key =True )    name = db .Column (db .String (20), unique =True )    phone = db .Column (db .String (20))    articles = db .relationship ('Article')  # collection ,这是⼀个集合关系属性,⾥⾯包含了作者的所有⽂章    def  __repr__(self ):        return  '<Author %r>' % self .name #最后有两种⽅法来建⽴:#第⼀种是“多”这⼀端,直接给外键赋值#第⼆种是“⼀”这⼀端,将关系属性赋值给实际对象class  Article (db .Model ):    id  = db .Column (db .Integer , primary_key =True )    title = db .Column (db .String (50), index =True )    body = db .Column (db .Text )    author_id = db .Column (db .Integer , db .ForeignKey ('author.id'))#⽤外键实现⼀对多的关系    def  __repr__(self ):        return  '<Article %r>' % self .title 1
2
3
4
5
6
7
89
10
11
12
13
14
15
16
17
18
19
20
21
22
# one to one #⼀对⼀就是⼀种特殊的⼀对多,只不过那个list ⾥的元素变成⼀个了⽽已class  Country (db .Model ):    id  = db .Column (db .Integer , primary_key =True )    name = db .Column (db .String (30), unique =True )    capital = db .relationship ('Capital', back_populates ='country', uselist =False )  # collection -> scalar    def  __repr__(self ):        return  '<Country %r>' % self .name class  Capital (db .Model ):    id  = db .Column (db .Integer , primary_key =True )    name = db .Column (db .String (30), unique =True )    country_id = db .Column (db .Integer , db .ForeignKey ('country.id'))    country = db .relationship ('Country', back_populates ='capital')  # scalar    def  __repr__(self ):        return  '<Capital %r>' % self .name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
借助关联表作为跳板,⼀⽅先对关联表进⾏检索,然后到对应的另⼀⽅信息。
三、总代码及注释# many to many with association table association_table = db .Table ('association',                            db .Column ('student_id', db .Integer , db .ForeignKey ('student.id')),                            db .Column ('teacher_id', db .Integer , db .ForeignKey ('teacher.id'))                            )class  Student (db .Model ):    id  = db .Column (db .Integer , primary_key =True )    name = db .Column (db .String (70), unique =True )    grade = db .Column (db .String (20))    teachers = db .relationship ('Teacher',   
                          secondary =association_table ,                              back_populates ='students')  # collection    def  __repr__(self ):        return  '<Student %r>' % self .name class  Teacher (db .Model ):    id  = db .Column (db .Integer , primary_key =True )    name = db .Column (db .String (70), unique =True )    office = db .Column (db .String (20))    students = db .relationship ('Student',                              secondary =association_table ,                              back_populates ='teachers')  # collection    def  __repr__(self ):        return  '<Teacher %r>' % self .name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31import  os import  sys import  click
1
2
3
4
5
import  click from  flask import  Flask from  flask import  redirect , url_for , abort , render_template , flash from  flask_sqlalchemy import  SQLAlchemy from  flask_wtf import  FlaskForm from  wtforms import  SubmitField , TextAreaField from  wtforms .validators import  DataRequired # SQLite URI compatible WIN = sys .platform .startswith ('win')if  WIN :    prefix = 'sqlite:///'else :    prefix = 'sqlite:'app = Flask (__name__)app .jinja_env .trim_blocks = True app .jinja_env .lstrip_blocks = True app .config ['SECRET_KEY'] = os .getenv ('SECRET_KEY', 'secret string')#配置数据库URI app .conf
ig ['SQLALCHEMY_DATABASE_URI'] = os .getenv ('DATABASE_URL', prefix + os .path .join (app .root_path , 'data.db'))app .config ['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy (app ) #实例化⼀个SQLAlchemy ,命名为db # handlers @app.shell_context_processor def  make_shell_context ():    return  dict (db =db , Note =Note , Author =Author , Article =Article , Writer =Writer , Book =Book ,                Singer =Singer , Song =Song , Citizen =Citizen , City =City , Capital =Capital ,                Country =Country , Teacher =Teacher , Student =Student , Post =Post , Comment =Comment , Draft =Draft )@app.climand()@click.option('--drop', is_flag =True , help='Create after drop.')def  initdb (drop ):    """Initialize the database."""    if  drop :        db .drop_all ()    db .create_all ()    click .echo ('Initialized database.')# Forms #创建三个表单,分别⽤于填写新笔记、编辑笔记和删除笔记class  NewNoteForm (FlaskForm ):    body = TextAreaField ('Body', validators =[DataRequired ()])    submit = SubmitField ('Save')class  EditNoteForm (FlaskForm ):    body = TextAreaField ('Body', validators =[DataRequired ()])    submit = SubmitField ('Update')class  DeleteNoteForm (FlaskForm ):    submit = SubmitField ('Delete')# Models class  Note (db .Model ):  #定义了⼀个Note 类,即后⾯要实例化的笔记本的⾏,这⾥包含⼀个id 作为主键,还有⼀个⽂本内容    id  = db .Column (db .Integer , primary_key =True )    body = db .Column (db .Text )
5678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
# optional    def  __repr__(self ):        return  '<Note %r>' % self .body @ute('/')def  index ():    form = DeleteNoteForm ()    notes = Note .query .all ()    return  render_template ('index.html', notes =notes , form =form )@ute('/new', methods =['GET', 'POST'])def  new_note ():#是⼀个⽐较典范的程序    form = NewNoteForm ()    if  form .validate_on_submit ():        body = form .body .data        note = Note (body =body )        db .session .add (note )        db .session mit ()        flash ('Your note is saved.')        return  redirect (url_for ('index'))    return  render_template ('new_note.html', form =form )@ute('/edit/<int:note_id>', methods =['GET', 'POST'])def  edit_note (note_id ):    form = EditNoteForm ()    note = Note .query .get (note_id )    if  form .validate_on_submit ():        note .body = form .body .data        db .session mit ()        flash ('Your note is updated.')        return  redirect (url_for ('index'))    form .body .data = note .body  # preset form input's value    return  render_template ('edit_note.html', form =form )@ute('/delete/<int:note_id>', methods =['POST'])def  delete_note (note_id ):    form = DeleteNoteForm ()    if  form .validate_on_submit ():        note = Note .query .get (note_id )#通过id 先到        db .session .delete (note )#然后直接删除掉        db .session mit ()        flash ('Your note is deleted.')    else :        abort (400)    return  redirect (url_for ('index'))# one to many class  Author (db .Model ):    id  = db .Column (db .Integer , primary_key =True )    name = db .Column (db .String (20), unique =True )    phone = db .Column (db .String (20))    articles = db .relationship ('Article')  # collection ,这是⼀个集合关系属性,⾥⾯包含了
作者的所有⽂章    def  __repr__(self ):        return  '<Author %r>' % self .name #最后有两种⽅法来建⽴:#第⼀种是“多”这⼀端,直接给外键赋值#第⼆种是“⼀”这⼀端,将关系属性赋值给实际对象class  Article (db .Model ):707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。