gpt4 book ai didi

python - 从模型 flask 、sqlalchamey 和 wtforms 创建表单

转载 作者:太空宇宙 更新时间:2023-11-04 08:06:03 25 4
gpt4 key购买 nike

我收到此错误,但我不确定自己做错了什么。我创建的表格就像我从互联网上得到的例子一样。我按照这个例子 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与单独的 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']

关于python - 从模型 flask 、sqlalchamey 和 wtforms 创建表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31088906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com