web2py and peewee model class

19 views
Skip to first unread message

黄祥

unread,
Mar 10, 2022, 8:48:38 AM3/10/22
to web2py-users
controlelrs/test.py
from peewee import *

db = SqliteDatabase('peewee.sqlite')

class BaseModel(Model):
    class Meta:
        database = db

class Person(BaseModel):
    name = CharField()

def peewee_create_tables():
    with db:
        db.create_tables([Person] )

def peewee_get():
    table_name = request.args(0)
    peewee_create_tables()
    all_columns = Person.select()
    all_columns_output = [e for e in all_columns.tuples() ]
    return str(all_columns_output)

objective
can pass the table name from curl terminal cli into request.args(0)

e.g.

all_columns = table_name.select()

result
Traceback (most recent call last):
  File "/Users/sugizo/Downloads/web2py/gluon/restricted.py", line 219, in restricted
    exec(ccode, environment)
  File "/Users/sugizo/Downloads/web2py/applications/test/controllers/test.py", line 95, in <module>
  File "/Users/sugizo/Downloads/web2py/gluon/globals.py", line 430, in <lambda>
    self._caller = lambda f: f()
  File "/Users/sugizo/Downloads/web2py/applications/test/controllers/test.py", line 47, in peewee_get
    all_columns = table_name.select()
AttributeError: 'str' object has no attribute 'select'

question
how to pass the request.args(0) into class <class 'peewee.ModelBase'> using web2py way ?

thanks and best regards,
stifan
Reply all
Reply to author
Forward
0 new messages