Creat
1 创建Python对象(实例化模型类)
2 添加新创建的记录到对数据库对话
3 提交数据库对话
from app import db, Note
note1 = Note(body=1)
db.session.add(note1)
db.session.commit()
首先从app模块导入db对象和Note类,然后创建note1对象实例,使用关键字参数传入字段数据。我们的Note继承自db model基类,db model基类会提供一个构造参数,接受匹配类的属性名称的参数值,并赋给对应的类属性,所以我们不用自己创建Note类的定义构造方法
类的构造方法指的是 class的 init_(self,**arg, **kwargs)这个方法
Read
<模型类>.query.<过滤方法>.<查询方法>
查询方法:
all
first
one
get
count
one_or_none
first_or_none
get_or_404
paginate
with_parent
过滤器:
查询过滤器名称
filter
filter_by
order_by
limit
group_by
offset
# smaples
Node.query.all()
Node.query.filter(Node.body=="SHAVE").first()
除了= , !=之外的逻辑判断表达式
LIke
IN
NOT IN
AND
OR
filter(Note.body.like('%foo%'))
filter(Note.body.in_([]))
filter(and_(Note.body=='foo', Note.title == 'FooBar'))
filter(or_(Note.body=='foo', Note.body=='bar'))
Updata
note = Note.query.get(2) # primary key == 2
note.body = 'someting you want'
db.commit()
Deletef
note = Note.query.get(2) # primary key == 2
db.session.delete(note)
db.commit()