我刚刚开始使用 web2py,确实无法通过 SQLFORM.grid 部分。 我的数据位于 mySQL 数据库中,我正在尝试调用它的查询。
我定义了表,但没有 id 字段,因为我将其称为“itemNumber”并将其用作我的 id 字段。
db.define_table('items',
Field('itemNumber', 'integer'),
Field('title', 'string'),
Field('price', 'integer'),
Field('numSold', 'integer'))
在我的default.py中
def display_form():
db.items.id.readable = False
query = (db.items.numSold)
fields = (db.items.price, db.items.numSold, db.items.title)
headers = {'items.price': 'Price', 'items.numSold': '# Sold', 'items.title': 'Title'}
default_sort_order = [db.items.numSold]
form = SQLFORM.grid(query=query, fields=fields, headers=headers, orderby=default_sort_order, create=False, deletable=False, editable=False, maxtextlength=64,
paginate = 25)
return dict(form=form)
html页面上提供的错误: 不支持查询:(1054,u“‘字段列表’中未知列‘items.id’”) 没有找到记录
我以为我读到 SQLFORM.grid 自动调用“.id”,但在我的例子中,如果唯一键被命名为 itemNumber 而不是 id 会怎样?
如何关闭它?
非常感谢
约翰
最佳答案
您的表定义应按如下方式开始:
db.define_table('items',
Field('itemNumber', 'id'),
当您指定字段类型为“id”时,web2py 会将该字段视为自增整数主键,并且不会创建名为“id”的单独字段。
关于python - Web2Py SQLFORM.Grid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21212383/