0

我是 Web2py 的新手,无法理解票证抛出的错误。有人可以解释错误及其发生的原因吗?

这是门票:

票号

127.0.0.1.2012-08-29.01-43-16.edfb1953-fd71-4aa6-a768-815fe46fe273 'Expression' 对象没有属性 'strip'

追溯

   回溯(最近一次通话最后):
      文件“/home/user/Web2py/web2py/gluon/restricted.py”,第 205 行,在环境中的受限 exec ccode 中
      文件“/home/user/Web2py/web2py/applications/SocialImage/models/db.py”,第 12 行,格式 = '%(title)s')
      文件“/home/user/Web2py/web2py/gluon/dal.py”,第 6320 行,在 define_table
    多模型=多模型)
      文件“/home/user/Web2py/web2py/gluon/dal.py”,第 598 行,create_table referenced = field.type[10:].strip()
   AttributeError:“Expression”对象没有属性“strip”
错误快照

('Expression' 对象没有属性 'strip')

函数参数列表

(self=, table=, 'id': }>, migrate=True, fake_migrate=False, polymodel=None)
代码清单

            可排序 += 1
            k = 字段名
            if isinstance(field.type,SQLCustomType):
                ftype = field.type.native 或 field.type.type
            elif field.type.startswith('reference'):
                引用 = field.type[10:].strip()

                约束名 = self.constraint_name(表名,字段名)
                如果有属性(表,'_primarykey'):
                    rtablename,rfieldname = referenced.split('.')
                    rtable = table._db[rtablename]

代码:

    db = DAL("sqlite://storage.sqlite")

    db.define_table('用户',
        字段('uname',unique=True),
        字段('名称'),
        字段('电子邮件'))

    db.define_table('图像',
        字段('标题',唯一=真),
        字段('文件','上传'),
        字段('uploader_name',db.user.uname),
        格式 = '%(title)s')

    db.define_table('评论',
        字段('image_id',db.image),
        字段('作者',db.user.uname),
        字段('正文','文本'))

    db.user.email.requires=IS_EMAIL()
    db.user.uname.requires=IS_NOT_IN_DB(db,db.uname)
    db.user.name=IS_NOT_EMPTY()
    db.image.uploader.requires=IS_IN_DB(db,db.user.uname)
    db.comment.image_id.requires=IS_IN_DB(db,db.image.id,'%(title)s')
    db.comment.image_id.writable=db.comment.image_id.readable=False
4

1 回答 1

1

您错误地定义了参考字段——它们应该引用一个表,而不是该表中的特定字段。例如,您有:

Field('uploader_name', db.user.uname)

应该是:

Field('uploader_name', db.user)

请注意,引用字段不会存储用户表中的实际 uname ——它将存储用户表中引用记录的 id。如果需要,您可以从该参考中获取 uname。

此外,即将发布的 web2py 2.0 版本包括一个新的“惰性表”特性,它可以惰性地定义表(完整定义被推迟到第一次通过 db.tablename 引用访问表时)。因此,为了让您的表保持惰性,定义引用字段的首选方法是:

Field('uploader_name', 'reference user')

这避免了使用db.user,这将立即强制定义用户表。

最后,向被引用的表添加“格式”属性是个好主意——默认情况下,引用字段使用被引用表的“格式”属性来确定如何在表单下拉列表中显示字段的值,网格/表格和只读表格。对于用户表,您可以执行以下操作:

db.define_table('user',
    Field('uname',unique=True),
    Field('name'),
    Field('email'),
    format='%(uname)s')

In that case, all fields that reference the user table will display the uname value instead of the underlying record id (even though the reference field itself will store the record id).

于 2012-08-28T21:20:53.550 回答