简体   繁体   中英

Can't order query results randomly with Peewee

I am using MySQL with Peewee. Everything has worked nicely, but now I can't order my query in a random order.

Based on the documentation I've tried the following code:

import peewee as pw
objz = featured.select().order_by(fn.Rand()).limit(5)

After calling the query I got the following error:

builtins.NameError

NameError: name 'fn' is not defined

So I would like to ask somebody who is more familiar with Peewee, that fn is something that I need to import or implement somehow? I already tried order_by(tablename.Rand()).limit(5) , but it didn't solve the issue.

I assume I made a beginner mistake somewhere, but I can't figure it out.

It should be

pw.fn.Rand()

or,

from peewee import fn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM