“根本不”不是定义的枚举值

问题描述 投票:0回答:2

我正在使用Flask和SQL Alchemy创建一个人们每两周完成一次的调查。该调查包含12个无线电场

以下是他们必须完成的调查表:

class SurveyForm(FlaskForm):
parent_name = StringField('Name:')
child_name = StringField('Child\'s name:')
q1 = RadioField('question goes here', choices=[('Not at all', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('Very', 10)], validators=[DataRequired()])
q2 = RadioField('question goes here', choices=[('Not at all', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('Very', 10)], validators=[DataRequired()])
q3 = RadioField('question goes here', choices=[('Not at all', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('Very', 10)], validators=[DataRequired()])
q4 = RadioField('question goes here', choices=[('Not at all', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('Very', 10)], validators=[DataRequired()])
q5 = RadioField('question goes here', choices=[('Not at all', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('Very', 10)], validators=[DataRequired()])
q6 = RadioField('question goes here', choices=[('Not at all', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('Very', 10)], validators=[DataRequired()])
q7 = RadioField('question goes here', choices=[('Not at all', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('Very', 10)], validators=[DataRequired()])
q8 = RadioField('question goes here', choices=[('Not at all', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('Very', 10)], validators=[DataRequired()])
q9 = RadioField('question goes here', choices=[('Not at all', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('Very', 10)], validators=[DataRequired()])
q10 = RadioField('question goes here', choices=[('Not at all', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('Very', 10)], validators=[DataRequired()])
q11 = RadioField('question goes here', choices=[('Not at all', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('Very', 10)], validators=[DataRequired()])
q12 = RadioField('question goes here', choices=[('Not at all', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('Very', 10)], validators=[DataRequired()])

这是模型/表:

class Survey(db.Model):

__tablename__ = 'survey'

id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
parent_name = db.Column(db.String)
child_name = db.Column(db.String)
q1 = sa.Column(qtype, nullable=False)
q2 = sa.Column(qtype, nullable=False)
q3 = sa.Column(qtype, nullable=False)
q4 = sa.Column(qtype, nullable=False)
q5 = sa.Column(qtype, nullable=False)
q6 = sa.Column(qtype, nullable=False)
q7 = sa.Column(qtype, nullable=False)
q8 = sa.Column(qtype, nullable=False)
q9 = sa.Column(qtype, nullable=False)
q10 = sa.Column(qtype, nullable=False)
q11 = sa.Column(qtype, nullable=False)
q12 = sa.Column(qtype, nullable=False)


def __init__(self, user_id, parent_name, child_name, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12):
    self.user_id = user_id,
    self.parent_name = parent_name,
    self.child_name = child_name,
    self.q1 = q1,
    self.q2 = q2,
    self.q3 = q3,
    self.q4 = q4,
    self.q5 = q5,
    self.q6 = q6,
    self.q7 = q7,
    self.q8 = q8,
    self.q9 = q9,
    self.q10 = q10,
    self.q11 = q11,
    self.q12 = q12

def __repr__(self):
    return f"{user_id} is {parent_name}"

以下是显示调查的视图功能:

@users.route('/survey', methods=['GET', 'POST'])
def survey():
    form = SurveyForm()

if form.validate_on_submit():

    user_id = form.user_id.data,
    parent_name = form.parent_name.data,
    child_name = form.child_name.data,
    q1 = form.q1.data,
    q2 = form.q2.data,
    q3 = form.q3.data,
    q4 = form.q4.data,
    q5 = form.q5.data,
    q6 = form.q6.data,
    q7 = form.q7.data,
    q8 = form.q8.data,
    q9 = form.q9.data,
    q10 = form.q10.data,
    q11 = form.q11.data,
    q12 = form.q12.data,

    respondee = Survey(user_id, parent_name, child_name, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12)

    db.session.add(respondee)
    db.session.commit()

    return redirect(url_for('login'))

return render_template('survey.html', form=form)

我想要的是:

我希望能够填写调查表并将信息存储在调查课程中

实际发生了什么:

当我点击带有调查表格的页面时,我收到一个错误(因此没有任何表格可见完成)。我收到错误,因为我在app.run中调试= True(deg = True)

完整的错误是:

sqlalchemy.exc.StatementError: (builtins.LookupError) "('Not at all',)" is not among the defined enum values
[SQL: INSERT INTO survey (user_id, parent_name, child_name, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]
[parameters: [{'q7': (('five',),), 'q9': (('two',),), 'q12': ('three',), 'q5': (('two',),), 'q2': (('five',),), 'parent_name': (('s',),), 'child_name': (('s',),), 'q4': (('two',),), 'q1': (('Not at all',),), 'q8': (('three',),), 'user_id': ((1,),), 'q11': (('three',),), 'q10': (('Not at all',),), 'q6': (('six',),), 'q3': (('three',),)}]]

感谢任何帮助或建议,因为我在网上找不到任何东西。谢谢!

python flask flask-sqlalchemy flask-wtforms
2个回答
1
投票

你似乎有两个问题。

  1. Enum类型要求在数据模型中指定允许的值。您应该使用允许的值创建Enum实例,并为每列重用它。见https://docs.sqlalchemy.org/en/13/core/type_basics.html#sqlalchemy.types.Enum

例如:

qtype = sa.Enum(('Not at all', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('Very', 10))

然后:

q1 = sa.Column(qtype, nullable=False)
  1. 此外,您的输入不提供实例化Survey实例的值,因此所有字段都获得值None。烧瓶形式的问题值是必需的,因此需要您的视图/控制器代码来提供更多反馈。我不是Flask专家,但我确实确定了以下内容

https://exploreflask.com/en/latest/forms.html?highlight=validate_on_submit

您需要调用表单验证方法,而不是仅仅引用它:

if form.validate_on_submit():
    respondee = Survey(parent_name = form.parent_name.data,
                       child_name = form.child_name.data,
                       q1 = form.q1.data,
                       q2 = form.q2.data,
                       q3 = form.q3.data,
                       q4 = form.q4.data,
                       q5 = form.q5.data,
                       q6 = form.q6.data,
                       q7 = form.q7.data,
                       q8 = form.q8.data,
                       q9 = form.q9.data,
                       q10 = form.q10.data,
                       q11 = form.q11.data,
                       q12 = form.q12.data
                       )

    db.session.add(respondee)
    db.session.commit()

0
投票

我设法通过简单地改变我的radiofield元组中的标签和值的顺序来纠正这个问题。愚蠢的疏忽但很容易这样做只是想更新这个以防万一其他人将来有这个问题。

我的选择现在是:选择= [(1,'1(根本没有)'),(2,2),(3,3),(4,4),(5,5),(6,6) ,(7,7),(8,8),(9,9),(10,'10(非常)')]并且错误没有出现。希望这有助于其他人。

© www.soinside.com 2019 - 2024. All rights reserved.