Python &引用;“一点也不”;不在定义的枚举值中

Python &引用;“一点也不”;不在定义的枚举值中,python,flask,flask-sqlalchemy,flask-wtforms,Python,Flask,Flask Sqlalchemy,Flask Wtforms,我正在使用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),

我正在使用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中有debug=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',),)}]]

如果您有任何帮助或建议,我将不胜感激,因为我在网上找不到任何东西。谢谢

您似乎有两个问题

  • 枚举类型要求在数据模型中指定允许的值。您应该使用允许的值创建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))
    
    然后:

  • 此外,您的输入没有提供值来实例化调查实例,因此所有字段都得到值None。flask表单要求提供问题值,因此需要您的视图/控制器代码来提供更多反馈。我不是烧瓶专家,但我在
  • 您需要调用表单验证方法,而不仅仅是引用它:

    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()
    

    最后,我通过简单地更改radiofield元组中标签和值的顺序来纠正这个问题。愚蠢的疏忽,但很容易做到这一点,只是想更新这一点,以防任何其他人在未来有这个问题

    我现在的选择是:
    选项=[(1,'1(一点也不);(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,'10(非常)]),错误不会出现。希望这对其他人有帮助

    嗨,韦斯利,谢谢你的反馈。关于你的第1点,我不确定在这段代码中我将如何做到这一点。老实说,可以用上面的代码给出一个简单的例子吗?关于第2点,我想你指的是我的视图函数,我把它放在这里,你可以看到。对不起,如果我误解了你的答案,因为我是新来的。谢谢我现在得到一个错误,说('根本不')不在定义的枚举值中。我已经用现在的代码(即您建议的代码)更新了代码。错误表明传递的是包含值的元组,而不仅仅是值。我对烧瓶不太熟悉,不知道是什么引起的。
    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()