python peewee - how to use distinct

Erans picture Erans · Jul 11, 2013 · Viewed 8.7k times · Source

I'm trying to get this code working with peewee:

distinct_list = QSales.select(QSales.account, QSales.tax_code).distinct().where(QSales.trans_num == 3717)
print distinct_list

but the print command result is:

<class '__main__.QSales'> SELECT DISTINCT t1.`Account`, t1.`Tax_Code` FROM `q_sales` AS t1 WHERE (t1.`Trans_#` = %s) [3717]

running the above select statement in MySQL editor (copy the print result to the editor) returns correct result.

I also tried:

distinct_list = QSales.select(fn.Distinct(QSales.account, QSales.tax_code)).where(QSales.trans_num == 3717)

but got the same result

What am I doing wrong?

Thank you.

Answer

Erans picture Erans · Jul 12, 2013

Sleeping over it, I realized that that code should be as follows:

distinct_list = QSales.select(QSales.account, QSales.tax_code).distinct().where(QSales.trans_num == 3717)
for item in distinct_list:
    print item.account
    print item.tax_code

This is closed now. Thank you.