Я использую 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',),)}]]
Буду признателен за любую помощь или совет, если смогу 'ничего не найти в Интернете.Спасибо!