【问题标题】:web2py - extra_fields in auth_user doesn't showweb2py - auth_user 中的 extra_fields 不显示
【发布时间】:2014-10-31 21:28:41
【问题描述】:

如果我创建一个新的简单 web2py 应用程序 test_auth 并使用此代码添加模型 my_tables.py 文件。

# -*- coding: utf-8 -*-
#Add authentication to the app.
from gluon.tools import Auth

auth = Auth(db)

#Add some fields in auth db to have a complet user profile.
auth.settings.extra_fields['auth_user']= [
    Field('phone_number', 'string'),
    Field('address', 'text'),
    Field('city', 'string'),
    Field('zip', 'string'),
    Field('phone', 'string'),
    Field('birthday', 'date', requires = IS_DATE(format=('%d-%m-%Y')))]
auth.define_tables(username=True, signature=True)

在控制器“default.py”中,我这样修改用户。

def user():
    if request.args(0) == 'profile':
        for field in auth.settings.extra_fields['auth_user']:
            field.readable = field.writable = True
    return dict(form=auth())

我在注册表单和表 auth_user 中看不到新字段。 我在某些地方错过了一些东西,但我看不到什么。

我使用 web2py 2.9.8 也用 2.9.5 测试过,我的行为完全一样。

【问题讨论】:

    标签: python python-2.7 web2py


    【解决方案1】:

    我在 my_tables.py 中的额外文件定义是问题所在。 Web2py 已经有了 db.py 模型中的 auth_user 定义。您需要自定义 db.py,而不是在单独的文件中添加新定义。

    【讨论】:

    • 注意,你可以把 Auth 代码放在一个单独的模型文件中——问题是你有相同的代码在两个不同的地方运行了两次——只要把它放在一个地方(不是必须是 db.py)。另外,请记住,模型文件将按字母顺序执行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2012-12-30
    • 2014-11-06
    • 1970-01-01
    • 2012-09-14
    相关资源
    最近更新 更多