Flask & SQLAlchemy - 3 table relationship commit

Viewed 40

I have the below 3 tables and I'm trying to commit some data into them but the line survey.question.choices(feedback=data['feedback']) is causing the error AttributeError: 'AppenderQuery' object has no attribute 'choices'.

Code:

survey = Survey.query.get(int(survey_id))
    if form.validate_on_submit():
         for data in formdata['fields']:
                        survey.question.question = data['question']
                        survey.question.choices.feedback = data['feedback']

Tables:

class Survey(db.Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(255), nullable=False)
    question = relationship('Question', backref='survey', lazy='dynamic')
    
class Question(db.Model):
    id = Column(Integer, primary_key=True)
    question = Column(String(255))
    survey_id = Column(Integer, ForeignKey('survey.id'), nullable=False)
    choices = relationship('Choice', backref='question', lazy='dynamic')

class Choice(db.Model):
    id = Column(Integer, primary_key=True)
    choice = Column(String(255))
    question_id = Column(Integer, ForeignKey('question.id'), nullable=False)
    feedback = Column(String(255))

I've also tried Question.choices(feedback = data['feedback']) but this gives me the error TypeError: 'InstrumentedAttribute' object is not callable

0 Answers
Related