flask的orm操作

时间:2023-03-10 03:34:40
flask的orm操作

django是有orm操作的  可想而知 那么flask也是有orm操作的,其实flask的orm操作的使用和djnago的是差不多的

django的orm操作进行条件筛选的时候后面跟着的是objects

django
  表名.objects.语句
flask的是query
  表名.objects.语句 eg:
  django:
    User.objects.filter(条件).first
  flask:
    User.query.filter_by(条件).first

常用查询语句:

  

all()     查询所有

filter_by / filter  单个查询
filter_by 不需要指定是哪个类的哪个属性,只需要制定属性及其目标值就可以了, 并且只能写具体的值不能写模糊值
filter filter中指定查询条件的时候需要指定类名的前缀。可以指定模糊值 order_by 排序

查询集

  1. 原始查询集

    类名.query得到的结果就为原始查询集

  2. 数据查询集

    加上各种的过滤器的方法 最终返回的结果 为数据查询集 都使用数据查询集

过滤器

(1) all 查询所有 以列表形式返回 不支持连贯操作

类名.query.all()

User.query.all()   # 查询User表中的所有数据

(2) filter() 过滤

类名.query.filter([类名.属性名 条件操作符 值])

 User.query.filter() #返回所有

 User.query.filter(User.age>) #查询年龄大于20的数据

 User.query.filter(User.age>,User.age<) #查询年龄大于20的数据 and 小于40

(3) filter_by 只支持参数为关键字参数

类名.query.filter_by(属性名=值...)

 data = User.query.filter_by(id=)

 data = User.query.filter_by(id>) #错误写法 不可以使用模糊查到

 data = User.query.filter_by(id=,age=)

(4) offset 偏移量

offset(num)

User.query.filter().offset()

(5) limit 取值

limit(num)

User.query.filter(User.age>).limit()   查到的结果只取两个

(6) offset和limit组合使用

 User.query.offset().limit()  也是只取两个

(7) order_by() 排序

默认是升序

    data = User.query.order_by(User.age) #升序
data = User.query.order_by(-User.age) #降序

(8) first 取出第一条数据 返回对象

User.query.first() == User.query.get()

(9) get 获取id对应的数据

查询成功返回对象 查询失败 返回None

User.query.get()

(10) contains 包含关系

User.query.filter(User.username.contains(''))    #username中包含数字7的数据

(11) like 模糊查询

 User.query.filter(User.username.like('李%')) #以李作为开头的

(12) startswith endswith 以...开头 以...结尾

 User.query.filter(User.username.startswith('李'))   # 以姓李的开头
User.query.filter(User.username.endswith('')) # 以6为结尾的

(13) 比较运算符

  1. __gt__
  2. __ge__
  3. __lt__
  4. __le__
  5. >
  6. <
  7. >=
  8. <=
  9. ==
  10. !=

.

(14) in 和 not in

User.query.filter(User.age.in_([,,,,,]))

(15) is null

User.query.filter(User.username.isnot(None))

(16) and_

多个条件 用逗号隔开,为and操作

from sqlalchemy import and_

 User.query.filter(and_(User.age==,User.id==))

(17) or_

from sqlalchemy import or_

@main.route('/and/')
def myAnd():
data = User.query.filter(or_(User.age==,User.id==))
data = User.query.filter(and_(User.username.like('%6%')),or_(User.age>=,User.id==))
return render_template('show.html',data=data)

(18) not_

from sqlalchemy import not_

@main.route('/and/')
def myAnd():
# data = User.query.filter(not_(User.age>,User.id==))\
#错误写法只能给一个条件取反
data = User.query.filter(not_(User.age>))
return render_template('show.html',data=data)

(19) count 统计

data = User.query.filter(not_(User.age>)).count()

四、文件的迁移

模块:

pip install flask-migrate

pip install flask-script

使用

(1) 实例化

from flask_migrate import Migrate,MigrateCommand
from flask_sqlalchemy import SQLalchemy
app = Flask(__name__)
db = SQLalchemy(app)
migrate = Migrate(app,db=db)
manager = Manager(app)
manager.add_command('db',MigrateCommand)

(2) 初始化 迁移文件目录

python manage.py db init

(3) 生成迁移文件

python manage.py db migrate

(4) 执行迁移文件

python manage.py db upgrade

注意

如果当前存在 模型 但是执行创建迁移文件的时候 提示没有任何改变的时候 需要查看当前的模型类是否有使用(导入)