从模型烧瓶、sqlalchamey 和 wtforms 创建表单

Creating forms from a model flask, sqlalchamey, and wtforms

我收到此错误,但我不确定自己做错了什么。我创建的表格就像我从互联网上得到的例子一样。我按照这个例子 https://wtforms-alchemy.readthedocs.org/en/latest/introduction.html#quickstart

class User(db.Model):
    __tablename__= 'users'

    DRIVER = 'driver'
    ADMIN = 'admin'

    username = db.Column('username', db.String(80), primary_key=True)
    _pw_hash = db.Column('pw_hash', db.String(128), nullable=False)
    _pw_salt = db.Column('pw_salt', db.String(20), nullable=False)
    first_name = db.Column('full_name', db.String(100), nullable=False)
    last_name = db.Column('last_name', db.String(100), nullable=False)
    email = db.Column('email', db.String(120))
    phone = db.Column('phone', db.String(20))
    #acct_type = db.Column('acct_type', db.Enum(DRIVER, ADMIN), nullable=False)

    def check_pw(self, pw):
        return self._pw_hash == util.pwhash(pw, self._pw_salt)

    @property
    def _pw_hash(self):
        return self._pw_hash

    @_pw_hash.setter
    def password(self, pw):
        self._pw_salt = util.salt(20)
        self._pw_hash = util.pwhash(pw, self._pw_salt)


    def __init__(self, un, pw, first, last, acct_type, email=None, phone=None):
        self.username = un
        self.password = pw
        self.first_name = first
        self.last_name = last
        self.acct_type = acct_type
        self.email = email
        self.phone = phone

UserFrom = model_form(User, base_class=Form, exclude=['_pw_hash'])

错误:

Traceback (most recent call last):
  File "/home/chase/workspace/T2T_Flask/model.py", line 1, in <module>
    from t2t import app
  File "/home/chase/workspace/T2T_Flask/t2t.py", line 4, in <module>
    from model import *
  File "/home/chase/workspace/T2T_Flask/model.py", line 48, in <module>
    UserFrom = model_form(User, base_class=Form, exclude=['_pw_hash', 'username'])
  File "/usr/local/lib/python3.4/dist-packages/wtforms/ext/appengine/db.py", line 460, in model_form
    field_dict = model_fields(model, only, exclude, field_args, converter)
  File "/usr/local/lib/python3.4/dist-packages/wtforms/ext/appengine/db.py", line 415, in model_fields
    props = model.properties()
AttributeError: type object 'User' has no attribute 'properties'

您错误地使用了 appengine 自动表单,而不是 sqlalchemy 表单。查看回溯中的路径:wtforms/ext/appengine/db.py。您似乎还混淆了 wtforms-sqlalchemy extension provided by the WTForms project with the separate WTForms-Alchemy 包。

如果您想使用内置支持:

from wtforms.ext.sqlalchemy.orm import model_form

UserForm = model_form(User, _exclude=['_pw_hash'])

如果您想使用单独的 WTForms-Alchemy 包:

from wtforms_alchemy import ModelForm

class UserForm(ModelForm):
    class Meta:
        model = User
        exclude = ['_pw_hash']