我正在使用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(deg = True)中打开了debug = 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',),)}]]
感谢您的帮助或建议,因为我在网上找不到任何东西。谢谢!
答案 0 :(得分:1)
您这里似乎有两个问题。
例如:
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)
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()
答案 1 :(得分:0)
最后,我仅通过更改无线电场元组中的标签顺序和值来设法纠正了这一问题。愚蠢的监督却很容易做到,只是想对它进行更新,以防将来任何其他人遇到此问题。
我现在的选择是: choices = [(1,'1(一点也不)'),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7 ),(8、8),(9、9),(10,'10(非常)')],并且不会出现错误。希望这对其他人有帮助。