Skip to Content
SQLAlchemy 核心指南,第 2 版
book

SQLAlchemy 核心指南,第 2 版

by Jason Myers, Rick Copeland
May 2025
Intermediate to advanced
208 pages
2h 36m
Chinese
O'Reilly Media, Inc.
Content preview from SQLAlchemy 核心指南,第 2 版

第 14 章 烹饪书

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

本章与前几章不同,每一节都侧重于使用 SQLAlchemy 的不同方面。这里的内容不像前几章那么详细;可以把它看作是有用工具的快速介绍。 在每一节的末尾,如果您有兴趣,可以了解更多信息。这些章节并不是完整的教程,而是介绍如何完成特定任务的简短食谱。本章的第一部分重点介绍了 SQLAlchemy 的一些高级用法,第二部分介绍了如何将 SQLAlchemy 与 Flask 等 Web 框架结合使用,最后一部分则介绍了可与 SQLAlchemy 结合使用的其他库。

混合属性

混合属性在作为类方法访问时表现出一种行为,而在实例上访问时又表现出另一种行为。另一种理解方式是,属性在 SQLAlchemy 语句中使用时将生成有效的 SQL,而在实例上访问时,混合属性将直接针对实例执行 Python 代码。我发现在查看代码时最容易理解这一点。在例 14-1 中,我们将使用Cookie 声明类来说明这一点。

例 14-1. 我们的 Cookie 用户数据模型
from datetime import datetime

from sqlalchemy import Column, Integer, Numeric, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method
from sqlalchemy.orm import sessionmaker


engine = create_engine('sqlite:///:memory:')

Base = declarative_base()


class Cookie(Base):
    __tablename__ = 'cookies'

    cookie_id = Column(Integer, primary_key=True)
    cookie_name = Column(String(50), index=True)
    cookie_recipe_url = Column(String(255))
    cookie_sku = Column(String(55))
    quantity = Column(Integer())
    unit_cost = Column(Numeric(12, 2))

    @hybrid_property 1
    def inventory_value(self):
        return self.unit_cost * self.quantity

    @hybrid_method 2
    def bake_more(self, min_quantity):
        return self.quantity < min_quantity

    def ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer
How to Overcome a Power Deficit

How to Overcome a Power Deficit

Cyril Bouquet, Jean-Louis Barsoux
The Human Factor in AI-Based Decision-Making

The Human Factor in AI-Based Decision-Making

Philip Meissner, Christoph Keding

Publisher Resources

ISBN: 9798341659087