Skip to content

Commit fa18184

Browse files
committed
Added a select random method to models
1 parent af54996 commit fa18184

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

backend/PyMatcha/utils/orm/_model.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,25 @@ def select_all(cls):
372372
for item in data:
373373
yield cls(item)
374374

375+
@classmethod
376+
def select_random(cls, count):
377+
logging.debug(f"Getting {count} random entries from {cls.table_name}")
378+
temp = cls()
379+
with temp.db.cursor() as c:
380+
c.execute(
381+
"""
382+
SELECT * FROM {}
383+
ORDER BY RAND()
384+
LIMIT {}
385+
""".format(
386+
temp.table_name, count
387+
)
388+
)
389+
data = c.fetchall()
390+
c.close()
391+
for item in data:
392+
yield cls(item)
393+
375394
@classmethod
376395
def drop_table(cls):
377396
logging.warning("Dropping table {}".format(cls.table_name))

0 commit comments

Comments
 (0)