joergwork
joergwork

Reputation: 301

How to "select distinct" with flask-restless

I'm new to flask-restless, and looking for a way to do a "SELECT DISTINCT" on a table. I've been reading the docs and found "Function evaluation". But I couldn't find how to put a function evaluation into a preprocessor or am I absolutely wrong? Does someone know a way how to do that?

Upvotes: 0

Views: 647

Answers (2)

koberone
koberone

Reputation: 27

You should use Custom queries.

Example:

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))

Upvotes: 0

TonyMoutaux
TonyMoutaux

Reputation: 357

Function evaluation only return values of computational functions, for exemple count, max, avg. I don't think it is the good way to dig in.

You should probably go with a custom query embedded in your class, as showed in the Custom Query 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))

Upvotes: 0

Related Questions