简体   繁体   中英

Creating forms from a model flask, sqlalchamey, and wtforms

I am getting this error and I am not sure what I am doing wrong. I created the form just like and example I got from the internet. I followed this example 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'])

Error:

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'

You've mistakenly used the appengine auto form, not the sqlalchemy one. Take a look at the path in the traceback: wtforms/ext/appengine/db.py . You also seem to be confusing the sqlalchemy support built in to WTForms with the separate WTForms-Alchemy package.

If you want to use the built-in support:

from wtforms.ext.sqlalchemy.orm import model_form

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

If you want to use the separate WTForms-Alchemy package:

from wtforms_alchemy import ModelForm

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM