flask学习笔记09

flask学习笔记09

过滤条件

首先,先建立一个数据库和数据表。

在这里插入图片描述

equals过滤
items = session.query(Shelf).filter(Shelf.author == '罗贯中').all()
for item in items:
    print(item)
{'id': 1, 'name': '三国演义', 'author': '罗贯中', 'pricr': Decimal('89.90'), 'status': '有货'}
not equals过滤
items = session.query(Shelf).filter(Shelf.author != '罗贯中').all()
for item in items:
    print(item)
{'id': 2, 'name': '水浒传', 'author': '施耐庵', 'pricr': Decimal('67.80'), 'status': '有货'}
{'id': 3, 'name': '西游记', 'author': '吴承恩', 'pricr': Decimal('99.90'), 'status': '无货'}
{'id': 4, 'name': '红楼梦', 'author': '曹雪芹', 'pricr': Decimal('80.00'), 'status': '有货'}
like过滤
items = session.query(Shelf).filter(Shelf.author.like('罗贯%')).all()
for item in items:
    print(item)
{'id': 1, 'name': '三国演义', 'author': '罗贯中', 'pricr': Decimal('89.90'), 'status': '有货'}

这种like方式有些像模糊搜索。

in_过滤
items = session.query(Shelf).filter(Shelf.author.in_(['罗贯中','曹雪芹'])).all()
for item in items:
    print(item)
{'id': 1, 'name': '三国演义', 'author': '罗贯中', 'pricr': Decimal('89.90'), 'status': '有货'}
{'id': 4, 'name': '红楼梦', 'author': '曹雪芹', 'pricr': Decimal('80.00'), 'status': '有货'}
notin_过滤

有in就有notin,只不过要注意notin有两种写法

items = session.query(Shelf).filter(~Shelf.author.in_(['罗贯中','曹雪芹'])).all()
for item in items:
    print(item)
items = session.query(Shelf).filter(Shelf.author.notin_(['罗贯中','曹雪芹'])).all()
for item in items:
    print(item)

结果是一样的

{'id': 2, 'name': '水浒传', 'author': '施耐庵', 'pricr': Decimal('67.80'), 'status': '有货'}
{'id': 3, 'name': '西游记', 'author': '吴承恩', 'pricr': Decimal('99.90'), 'status': '无货'}
{'id': 2, 'name': '水浒传', 'author': '施耐庵', 'pricr': Decimal('67.80'), 'status': '有货'}
{'id': 3, 'name': '西游记', 'author': '吴承恩', 'pricr': Decimal('99.90'), 'status': '无货'}
is_过滤
items = session.query(Shelf).filter(Shelf.status.is_('None')).all()
for item in items:
    print(item)
and过滤

and过滤有三种写法

items = session.query(Shelf).filter(Shelf.status == '有货', Shelf.author == '施耐庵').all()
for item in items:
    print(item)
items = session.query(Shelf).filter(Shelf.status == '有货').filter(Shelf.author == '施耐庵').all()
for item in items:
    print(item)
items = session.query(Shelf).filter(and_(Shelf.status == '有货',Shelf.author == '施耐庵')).all()
for item in items:
    print(item)

上面三个语句都是一样的结果

{'id': 2, 'name': '水浒传', 'author': '施耐庵', 'pricr': Decimal('67.80'), 'status': '有货'}
or_过滤
items = session.query(Shelf).filter(or_(Shelf.status == '有货',Shelf.author == '施耐庵')).all()
for item in items:
    print(item)
外键约束

大家可以把他理解成一种关系,像我下面写的书籍和作者的关系。

class Author(Base):
    __tablename__ = 'author'
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(25), nullable=False)

    def __str__(self):
        return str(dict(zip(['id', 'name'],[self.id, self.name])))
class Book(Base):
    __tablename__ = 'book'
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(25), nullable=False)
    price = Column(DECIMAL(5, 2))
    status = Column(Enum('有货', '无货'))
    uid = Column(Integer,ForeignKey('author.id', ondelete='RESTRICT'))

uid = Column(Integer,ForeignKey(‘author.id’, ondelete=‘RESTRICT’))这条语句就把两个表联合起来了

我们看一下这两个表格

在这里插入图片描述

在这里插入图片描述

我们还要注意外键约束

RESTRICT

父表数据被删除时会阻止删除并报错,这个约束也是默认约束。

uid = Column(Integer,ForeignKey('author.id', ondelete='RESTRICT'))

在这里插入图片描述

NO ACTION

几乎同上,

CASCADE

级联操作,删除父表的记录,子表的记录也会删除

在这里插入图片描述

在这里插入图片描述

SET NULL

父表记录被删除,子表记录的uid会设置为NULL

在这里插入图片描述

一对多

其实一对多就是一个父表对应多个子表,和多个子类继承同一个父类一样的思路。

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值