【问题标题】:How to "select distinct" with flask-restless如何用烧瓶不安“选择不同的”
【发布时间】:2014-03-28 01:14:29
【问题描述】:

我不熟悉烧瓶,正在寻找一种在桌子上执行“SELECT DISTINCT”的方法。我一直在阅读文档并找到“功能评估”。但是我找不到如何将函数评估放入预处理器中,还是我绝对错了? 有人知道怎么做吗?

【问题讨论】:

    标签: flask-restless


    【解决方案1】:

    您应该使用自定义查询。

    例子:

    class Employee(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode)
    
        @classmethod
        def get_unique_values(cls):
            return db.session.query(func.distinct(Employee.name))
    

    【讨论】:

      【解决方案2】:

      函数评估仅返回计算函数的值,例如 count、max、avg。我不认为这是挖掘的好方法。

      您可能应该使用嵌入在您的类中的自定义查询,如自定义查询https://flask-restless.readthedocs.org/en/latest/customizing.html#custom-queries中所示

      from sqlalchemy import distinct
      
      class Person(Base):
          __tablename__ = 'person'
          id = Column(Integer, primary_key=True)
          name = Column(Unicode(50))
      
          @classmethod
          def query(cls):
      
              return cls.query(func.distinct(Person.name))
      

      【讨论】:

        猜你喜欢
        • 2016-09-12
        • 1970-01-01
        • 2020-03-13
        • 1970-01-01
        • 2014-06-18
        • 1970-01-01
        • 2015-09-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多