Reputation: 21
This is my first Flask project and I am trying to stick to MVC and not just start writing SQL which is proving difficult. Converting this simple query to Flask-SQLAlchemy is a puzzle I am currently unable to solve.
SELECT DISTINCT gradelevel, beginningorendtest
FROM wordlist
ORDER BY gradelevel,beginningorendoftest
I appreciate any ideas, it may not be possible with Flask-SQLAlchemy. Here is my latest attempt.
data = wordlist.query.add_columns(wordlist.gradelevel, wordlist.beginningorendtest).distinct()
print (data)
for row in data:
print ("Gradelevel:" + row.gradelevel + " BeginningorEndoftest:" + row.beginningorendtest)
Upvotes: 1
Views: 2937
Reputation: 21
After mangling it for awhile, this:
data = wordlist.query.with_entities(wordlist.gradelevel, wordlist.beginningorendtest)
.distinct().order_by(wordlist.gradelevel).order_by(wordlist.beginningorendtest)
will produce this equivalent query:
SELECT DISTINCT wordlist.gradelevel AS wordlist_gradelevel,wordlist.beginningorendtest AS wordlist_beginningorendtest
FROM wordlist ORDER BY wordlist.gradelevel, wordlist.beginningorendtest
Upvotes: 1