在 Flask 中使用 SQLAlchemy 时,您可以使用 distinct() 方法来获取所有 ceduan 项不同的数据。以下是一个示例代码:

from flask import g
from your_app.models import HandleGaochabiaoMatchs
from sqlalchemy import and_

ceduan_list = HandleGaochabiaoMatchs.query.filter(
    and_(
        HandleGaochabiaoMatchs.user_id == g.user.user_id,
        HandleGaochabiaoMatchs.zhenduans
    )
).with_entities(HandleGaochabiaoMatchs.ceduan).distinct().all()

# 提取不同的 ceduan 项
distinct_ceduan_list = [item[0] for item in ceduan_list]

在这个代码示例中:

  1. ceduan_list 是一个包含所有不同 ceduan 项的列表,您可以在循环中遍历它以访问每个值。
  2. 您可以使用列表推导式从 ceduan_list 中提取 ceduan 的值,并将它们存储在 distinct_ceduan_list 中。

请注意:

  • 请将 your_app.models 替换为您应用程序的模型文件路径。
  • 确保您的数据库中存在名为 HandleGaochabiaoMatchs 的模型类。
Flask SQLAlchemy 获取所有 ceduan 项不同的数据 - distinct() 方法

原文地址: https://www.cveoy.top/t/topic/qe0p 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录